简体   繁体   中英

How do I pass an additional jar do my java command when running application?

I have a java gradle projectA that references another java gradle projectB, that builds as a lib.

My gradle build configuration seems to be fine, since I can import and use classes from the other project, and it compiles. But when I try to run the application from the command line I get an error...

Exception in thread "main" java.lang.NoClassDefFoundError: dawcore/SamplerInstrument at DawCLI.main(DawCLI.java:17) Caused by: java.lang.ClassNotFoundException: dawcore.SamplerInstrument at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more

It seems to be complaining that it can't load the classes in the jar at runtime....which makes sense. But I don't know how to have it successfully load those classes.

my current run command that does not reference the jar, is as follows....

/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/" DawCLI

Running this gives the initially mentioned error.

I then read the docs on java -cp argument. It says to provide additional classpath directories, to separate them by semicolon.

I susequently updated my run command to be as follows....

/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/;../DawCore/build/libs/DawCore.jar" DawCLI

This gives me the following error....

Error: Could not find or load main class DawCLI

My main function is as follows..

import dawcore.*;

public class DawCLI {
    public static void main (String [] args) throws IOException {
        SamplerInstrument samplerkick = new SamplerInstrument();
    }
}

According to the docs I seem to be doing this correctly, but am still getting errors. Any insight on this would be greatly appreciated!

Thanks

The solution was to replace the delimiting semi-colon with a colon instead. Even though the first line of the javadocs on -cp command indicate that you should use a semi-colon.

This could be because I am running on linux and those docs indicate Windows.

https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM