开发者

Java Classpath Woes

开发者 https://www.devze.com 2023-03-24 12:45 出处:网络
Luckily or unluckily, I haven\'t had to work too extensively with invoking java from the commandline up until this point, I\'ve usually been using something like Maven, Ant, or running things within a

Luckily or unluckily, I haven't had to work too extensively with invoking java from the commandline up until this point, I've usually been using something like Maven, Ant, or running things within a servlet container. I've just compiled my application in Maven into one JAR using the assembly:single goal, and am having serious problems running it from the commandline.

Here's what I'm attempting to do:

export JAVA_CLASSPATH="`pwd`:/path/to/remote/libs/"
java -cp "${JAVA_CLASSPATH}" -jar groupId-artifactId-version-jar-with-dependencies.jar com.my.main.Class

This is failing with the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/remote/lib/IServer
Caused by: java.lang.ClassNotFoundException: com.remote.lib.IServer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: com.my.main.Class. Program will exit.

The IServer class is in the /path开发者_如何转开发/to/remote/libs directory and isn't being found. It also seems to not be able to find the main class, which is really odd. What am I doing wrong?


The -jar option ignores the classpath ( and all other ) options.

You need to edit the manifest file in your jar and set the Class-path variable in there.

http://download.oracle.com/javase/1.4.2/docs/tooldocs/linux/java.html


@Kal's answer is correct, but an easier way to fix it would be this:

export JAVA_CLASSPATH="groupId-artifactId-version-jar-with-depencies.jar:`pwd`:/path/to/remote/libs/iserver.jar"
java -cp "${JAVA_CLASSPATH}" com.my.main.Class

No need to tinker with the manifest file; just add the jar you want to the classpath.

Also, if the IServer stuff is in a jar inside /path/to/remove/libs/ then you need to explicitly include the name of the jar file. I've included it above and assumed it is called "iserver.jar"

If you have many dependencies then it's often easier to write a shell script for launching your code. The script can build a (potentially very large) classpath string using shell glob goodness. The java executable doesn't do any globbing.

EDIT: Note that I've assumed that you don't have access to Ant or Maven or any of that good stuff, since you stated that you usually use those tools but are not doing it right now.


The classpath parameter must be specified with the exact path to the jar file. If your IServer class is found in server.jar, you need to specify "$pwd\:/path/to/remote/libs/server.jar", for example. (in other words, you need to include a :-separated list of paths to jar files)

As others have noted, if you choose this method you should remove the -jar parameter and instead include the class name you intend to be your main class on the command line, such as java -cp $PWD:/path/to/server.jar com.remote.lib.IServer.

Side note: usually names that start with I are reserved for interfaces; it seems a bit odd to use an interface as your main class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号