@@ -377,28 +377,41 @@ public static void createImage(JlinkConfiguration config,
377
377
// the token for "all modules on the module path"
378
378
private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH" ;
379
379
private JlinkConfiguration initJlinkConfig () throws BadArgs {
380
- ModuleFinder initialFinder = createFinderFromPath (options .modulePath );
381
- boolean isLinkFromRuntime = determineLinkFromRuntime (initialFinder , options .modulePath );
382
- ModuleFinder rootsFinder = isLinkFromRuntime ? ModuleFinder .compose (ModuleFinder .ofSystem (),
383
- initialFinder ) :
384
- initialFinder ;
385
- if (rootsFinder .find ("java.base" ).isEmpty ()) {
386
- assert !isLinkFromRuntime : "Expected regular JMODs based link" ;
387
- // External module linked into a custom runtime, but JDK modules
388
- // not observable on the module path. Add the default module path
389
- // if that exists.
380
+ ModuleFinder appModulePathFinder = createFinderFromPath (options .modulePath );
381
+ ModuleFinder finder = appModulePathFinder ;
382
+ boolean isLinkFromRuntime = false ;
383
+ if (!appModulePathFinder .find ("java.base" ).isPresent ()) {
384
+ // If the application module path finder doesn't contain the
385
+ // java.base module we have one of two cases:
386
+ // 1. A custom module is being linked into a runtime, but the JDK
387
+ // modules have not been provided on the module path.
388
+ // 2. We have a run-time image based link.
389
+ //
390
+ // Distinguish case 2 by adding the default 'jmods' folder and try
391
+ // the look-up again. For case 1 this will now find java.base, but
392
+ // not for case 2, since the jmods folder is not there or doesn't
393
+ // include the java.base module.
390
394
Path defModPath = getDefaultModulePath ();
391
395
if (defModPath != null ) {
392
396
options .modulePath .add (defModPath );
393
- rootsFinder = createFinderFromPath (options .modulePath );
397
+ finder = createFinderFromPath (options .modulePath );
398
+ }
399
+ // We've just added the default module path ('jmods'). If we still
400
+ // don't find java.base, we must resolve JDK modules from the
401
+ // current run-time image.
402
+ if (!finder .find ("java.base" ).isPresent ()) {
403
+ isLinkFromRuntime = true ;
404
+ // JDK modules come from the system module path
405
+ finder = ModuleFinder .compose (ModuleFinder .ofSystem (), appModulePathFinder );
394
406
}
395
407
}
396
408
409
+ // Determine the roots set
397
410
Set <String > roots = new HashSet <>();
398
411
for (String mod : options .addMods ) {
399
- if (mod .equals (ALL_MODULE_PATH ) && options . modulePath . size () > 0 ) {
400
- ModuleFinder mf = newModuleFinder (rootsFinder , options .limitMods , Set . of (), isLinkFromRuntime );
401
- // all observable modules are roots
412
+ if (mod .equals (ALL_MODULE_PATH )) {
413
+ ModuleFinder mf = newModuleFinder (finder , options .limitMods ,
414
+ Set . of (), isLinkFromRuntime );
402
415
mf .findAll ()
403
416
.stream ()
404
417
.map (ModuleReference ::descriptor )
@@ -408,8 +421,7 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
408
421
roots .add (mod );
409
422
}
410
423
}
411
-
412
- ModuleFinder finder = newModuleFinder (rootsFinder , options .limitMods , roots , isLinkFromRuntime );
424
+ finder = newModuleFinder (finder , options .limitMods , roots , isLinkFromRuntime );
413
425
414
426
// --keep-packaged-modules doesn't make sense as we are not linking
415
427
// from packaged modules to begin with.
@@ -425,39 +437,6 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
425
437
options .generateLinkableRuntime );
426
438
}
427
439
428
- /*
429
- * Determine whether or not JDK modules should be resolved from the run-time
430
- * image.
431
- *
432
- * @return {@code false} if JMODs including java.base are present on the
433
- * module path or in the default module path ('jmods'). {@code true}
434
- * otherwise.
435
- */
436
- private static boolean determineLinkFromRuntime (ModuleFinder initialFinder ,
437
- List <Path > modulePath ) {
438
- boolean linkFromRuntime = false ;
439
- if (!initialFinder .find ("java.base" ).isPresent ()) {
440
- // If the initial finder doesn't contain the java.base module
441
- // we have one of two cases:
442
- // 1. A custom module is being linked into a runtime, but the JDK
443
- // modules have not been provided.
444
- // 2. We have a run-time image based link.
445
- //
446
- // Distinguish case 2 by adding the default 'jmods' folder and try
447
- // the look-up again. For case 1 this will now find java.base, but
448
- // not for case 2, since the jmods folder is not there or doesn't
449
- // include the java.base module.
450
- List <Path > pathCopy = new ArrayList <>(modulePath );
451
- Path defaultPath = getDefaultModulePath ();
452
- if (defaultPath != null ) {
453
- pathCopy .add (defaultPath );
454
- }
455
- ModuleFinder finder = createFinderFromPath (pathCopy );
456
- linkFromRuntime = !finder .find ("java.base" ).isPresent ();
457
- }
458
- return linkFromRuntime ;
459
- }
460
-
461
440
/*
462
441
* Creates a ModuleFinder for the given module paths.
463
442
*/
0 commit comments