6

I have a jar built before Java 9. I'm trying to run a class that is in that jar using Java 9.

From all I read about automatic modules, this should work:

java -p lib/Legacy-1.3.0.jar -m Legacy/com.blah.MyClass

But instead I get this:

Error: Unable to initialize main class com.blah.MyClass in module module Legacy
Caused by: module Legacy: java.lang.NoClassDefFoundError

Yes, com.blah.MyClass is in Legacy. Can I run a class from an automatic module? Why is the word module repeated twice in that error message above?

If I run java --list-modules -p lib/Legacy-1.3.0.jar I see:

Legacy@1.3.0 file://path/to/jar/Legacy-1.3.0.jar automatic

If I run jdeps --generate-module-info . lib/Legacy-1.3.0.jar, I get:

module Legacy {
    requires java.logging;

    requires transitive java.activation;
    requires transitive java.xml;
    requires transitive java.xml.bind;
    requires transitive java.xml.ws;

    exports com.blah;
}
Bob Woodley
  • 1,246
  • 15
  • 30
  • 1
    Is your goal to just run it, or do you specifically want to try and run it as a module? Would it be good enough to just use the 'old' way of running things, so using `java -cp lib/Legacy-1.3.0.jar com.blah.MyClass`? – Mark Rotteveel Sep 04 '19 at 17:25
  • Aside: Can you also add to the question if you are able to generate the module-info using `jdeps --generate-module-info . Legacy-1.3.0.jar` and/or what is the result of `jar --describe-module --file=lib/Legacy-1.3.0.jar`? – Naman Sep 04 '19 at 17:28
  • The reason I'm not doing it the old way is because the legacy jar has dependencies and it is nice to have them automatically pulled in using the new module based way of doing things. – Bob Woodley Sep 04 '19 at 17:29
  • Add `jdeps` to answer. – Bob Woodley Sep 04 '19 at 17:55
  • Does following these steps work: 1. Creating module `jar --create --file=lib/Legacy-1.3.0.jar \ --main-class=com.blah.MyClass -C mods/Legacy` 2. Then executing it using `java -p lib -m Legacy` ? – Naman Sep 04 '19 at 17:59
  • @naman - no, gets `Error parsing file arguments`. – Bob Woodley Sep 04 '19 at 18:43
  • 1
    You asked "Can I run a class from an automatic module? Why is the word module repeated twice in that error message above?". Yes it should work although it is a very unusual way to run an application when you don't have any explicit modules on the module path. Also the "module module" error message has fixed in JDK 13 via JDK-8221368. – Alan Bateman Sep 05 '19 at 07:19
  • 3
    Just on the NoClassDefFoundError, it might be that there is another error that is hidden. Does `java -cp lib/Legacy-1.3.0.jar com.blah.MyClass` work? I'm curious if it reveals other errors. – Alan Bateman Sep 05 '19 at 07:22

0 Answers0