I have written source codes of two modules in the directory ~/Desktop/src
code of the module myModuleA
M1.java
package myPack1;
import myPack2.M2;
public class M1
{
public static void main(String[] args)
{
System.out.println("I am M1");
M2.print();
System.out.println("I am M1 Again");
}
}
module-info.java
module myModuleA
{
requires myModuleB;
}
code of the module myModuleB
M2.java
package myPack2;
public class M2
{
public static void print()
{
System.out.println("I am M2");
}
}
module-info.java
module myModuleB
{
exports myPack2;
}
I compiled both modules as:
gyan@#ns:~/Desktop$ javac --module-source-path src -d out -m myModuleA,myModuleB
The directory out is created on the Desktop, which is my current directory.
Thereafter I created another directory on the Desktop with name OtherModule. Cut and paste the compile module directory of myModuleB from the directory out to the directory OtherModule. Deleted the source directory of the module myModuleB from the directory src. Deleted the directory out.
Again I compiled the module myModuleA with the compiled code of the module myModuleB as:
gyan@#ns:~/Desktop$ javac --module-source-path src --module-path OtherModule -d out -m myModuleA
And executed the code successfully as:
gyan@#ns:~/Desktop$ java --module-path out/:OtherModule/ -m myModuleA/myPack1.M1
I am M1
I am M2
I am M1 Again
gyan@#ns:~/Desktop$
But then I changed the module-info.java file of the module myModuleA as:
module myModuleA
{
requires static myModuleB;
}
Again I deleted the directory out and compiled the code as:
gyan@#ns:~/Desktop$ javac --module-source-path src --module-path OtherModule -d out -m myModuleA
The code compiles successfully. Then I tried to execute the code as:
gyan@#ns:~/Desktop$ java --module-path out/:OtherModule/ -m myModuleA/myPack1.M1
I am M1
Exception in thread "main" java.lang.NoClassDefFoundError: myPack2/M2
at myModuleA/myPack1.M1.main(M1.java:8)
Caused by: java.lang.ClassNotFoundException: myPack2.M2
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 1 more
gyan@#ns:~/Desktop$
What is the reason behind this error, even if I am providing the path to the both compiled modules.
--upgrade-module-pathin use for your example? What happens if you simply let it be the--module-path?--add-module myModuleBto the command line and I assume it will work. BTW: What is the reason for using --upgrade-module-path in these examples? Which JDK modules are you overriding.