I create the simplest possible modular project in Eclipse 4.21.0 (see below), with two source folders and one package. Both main
methods just print "Hello".
- If I right-click on
Main1
and Run as Java Application, it's all fine. - If I right-click on
Main2
and do the same, I get:
Error: Could not find or load main class p1.Main2
Caused by: java.lang.ClassNotFoundException: p1.Main2
If I check the two run configurations this generated (Main1
and Main2
), they are (superficially) identical, except for the name of the Main class. If I go into the Main1
launch configuration and change the main class to Main2
, now it works. So Eclipse generated a valid launch config for the top source folder, and a broken one for the second source folder.
Why is this, and how can I generate valid launch configs for other source folders?
Details
- I'm aware that this is not an issue for non-modular projects, but I'm interested in solving the case for modular projects.
- This happens even when I delete/refresh the launch config before launching, and independently of the order I run
Main1
andMain2
. - This happens whether I have a single output folder or one per source folder.
CodePudding user response:
This turns out to be a known bug reported on 2020-11-23 for version 4.17: Cannot launch class if module-info.java is in additional source directory.
Workaround
As indicated in the original bug report, attempting to modify the broken configuration does not solve the problem:
If I edit the launch configuration (even if I just change the name) I get the following error instead on launching:
Error occurred during initialization of boot layer...
However, it is possible to create a valid configuration for the main source folder (where module-info.java
is located), and modify that configuration to use a main class from the additional source folder, which will run fine.