1

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.

6
  • What for is the --upgrade-module-path in use for your example? What happens if you simply let it be the --module-path? Commented Jan 26, 2018 at 11:34
  • 2
    myModuleB is observable but it will not be resolved unless some only modules requires it. If myModuleB is resolved then myModuleA will read it). So add --add-module myModuleB to 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. Commented Jan 26, 2018 at 11:35
  • 1
    @nullpointer How is this a duplicate of that question? That one asks "Does X exist?", this one "When doing X, why doesn't Y work?". Even though their answers might overlap partially, these are totally different angles and no one having the OP's problem will assume the answer is hidden in "Does X exist?". Commented Jan 26, 2018 at 11:52
  • @AlanBateman Yes It works. And no need to use --upgrade-module-path. We can use --module-path instead. This was stored command in my terminal, so I was reusing it. I am going to change my question and use --module-path instead of --upgrade-module-path. Thanks. Commented Jan 26, 2018 at 11:53
  • @nullpointer As Nicolai said this is totally different question. I already have seen that question before writing this one. Commented Jan 26, 2018 at 11:59

1 Answer 1

2

Optional dependencies are not resolved on their own - even if they are present on the module path. If they are not resolved, they don't make it into the readability graph and so they aren't available at run time - hence the error message.

For the optional dependency to be available at run time, it must either be non-optionally required by third module, resolved during service binding, or added manually with --add-modules.

Sign up to request clarification or add additional context in comments.

2 Comments

really --add-exports? The anchor of your link hints at --add-module, which makes more sense (though when I follow the link the anchor doesn't appear to exist).
@StephanHerrmann, thanks for pointing those mistakes out, I fixed them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.