@@ -277,14 +277,6 @@ int run(String[] args) {
277
277
return EXIT_OK ;
278
278
}
279
279
280
- if (options .modulePath .isEmpty ()) {
281
- // no --module-path specified - try to set $JAVA_HOME/jmods if that exists
282
- Path jmods = getDefaultModulePath ();
283
- if (jmods != null ) {
284
- options .modulePath .add (jmods );
285
- }
286
- }
287
-
288
280
JlinkConfiguration config = initJlinkConfig ();
289
281
outputPath = config .getOutput ();
290
282
if (options .suggestProviders ) {
@@ -377,8 +369,13 @@ public static void createImage(JlinkConfiguration config,
377
369
// the token for "all modules on the module path"
378
370
private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH" ;
379
371
private JlinkConfiguration initJlinkConfig () throws BadArgs {
372
+ // Empty module path not allowed with ALL-MODULE-PATH in --add-modules
373
+ if (options .addMods .contains (ALL_MODULE_PATH ) && options .modulePath .isEmpty ()) {
374
+ throw taskHelper .newBadArgs ("err.no.module.path" );
375
+ }
380
376
ModuleFinder appModuleFinder = newModuleFinder (options .modulePath );
381
377
ModuleFinder finder = appModuleFinder ;
378
+
382
379
boolean isLinkFromRuntime = false ;
383
380
if (!appModuleFinder .find ("java.base" ).isPresent ()) {
384
381
// If the application module finder doesn't contain the
@@ -393,8 +390,9 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
393
390
// include the java.base module.
394
391
Path defModPath = getDefaultModulePath ();
395
392
if (defModPath != null ) {
396
- options .modulePath .add (defModPath );
397
- finder = newModuleFinder (options .modulePath );
393
+ List <Path > combinedPaths = new ArrayList <>(options .modulePath );
394
+ combinedPaths .add (defModPath );
395
+ finder = newModuleFinder (combinedPaths );
398
396
}
399
397
// We've just added the default module path ('jmods'). If we still
400
398
// don't find java.base, we must resolve JDK modules from the
@@ -419,8 +417,31 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
419
417
Set <String > roots = new HashSet <>();
420
418
for (String mod : options .addMods ) {
421
419
if (mod .equals (ALL_MODULE_PATH )) {
422
- ModuleFinder mf = newLimitedFinder (finder , options .limitMods ,
423
- Set .of ());
420
+ // Using --limit-modules with ALL-MODULE-PATH is an error
421
+ if (!options .limitMods .isEmpty ()) {
422
+ throw taskHelper .newBadArgs ("err.limit.modules" );
423
+ }
424
+ // all observable modules in the app module path are roots
425
+ Set <String > initialRoots = appModuleFinder .findAll ()
426
+ .stream ()
427
+ .map (ModuleReference ::descriptor )
428
+ .map (ModuleDescriptor ::name )
429
+ .collect (Collectors .toSet ());
430
+
431
+ // Error if no module is found on the app module path
432
+ if (initialRoots .isEmpty ()) {
433
+ String modPath = options .modulePath .stream ()
434
+ .map (a -> a .toString ())
435
+ .collect (Collectors .joining (", " ));
436
+ throw taskHelper .newBadArgs ("err.empty.module.path" , modPath );
437
+ }
438
+
439
+ // Use a module finder with limited observability, as determined
440
+ // by initialRoots, to find the observable modules from the
441
+ // application module path (--module-path option) only. We must
442
+ // not include JDK modules from the default module path or the
443
+ // run-time image.
444
+ ModuleFinder mf = limitFinder (finder , initialRoots , Set .of ());
424
445
mf .findAll ()
425
446
.stream ()
426
447
.map (ModuleReference ::descriptor )
@@ -430,7 +451,7 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
430
451
roots .add (mod );
431
452
}
432
453
}
433
- finder = newLimitedFinder (finder , options .limitMods , roots );
454
+ finder = limitFinder (finder , options .limitMods , roots );
434
455
435
456
// --keep-packaged-modules doesn't make sense as we are not linking
436
457
// from packaged modules to begin with.
@@ -497,7 +518,7 @@ public static Path getDefaultModulePath() {
497
518
* specified in {@code limitMods} plus other modules specified in the
498
519
* {@code roots} set.
499
520
*/
500
- public static ModuleFinder newLimitedFinder (ModuleFinder finder ,
521
+ public static ModuleFinder limitFinder (ModuleFinder finder ,
501
522
Set <String > limitMods ,
502
523
Set <String > roots ) {
503
524
// if limitMods is specified then limit the universe
0 commit comments