简体   繁体   中英

Why occurs a java.lang.NoClassDefFoundError when I use args4j in a batch built by mvn:assembly

For various experimentations, I take care of a java project in github .

After the Maven build, the program runs with a script bat.

Now I opened a branch because I would use the library args4j to parsing the arguments.

The build works fine, the jars exist in the directory lib , but when I run I have this stacktrace of Exception

Exception in thread "main" java.lang.NoClassDefFoundError: org/kohsuke/args4j/CmdLineException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2625) at java.lang.Class.getMethod0(Class.java:2866) at java.lang.Class.getMethod(Class.java:1676) at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486) Caused by: java.lang.ClassNotFoundException: org.kohsuke.args4j.CmdLineException at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ... 6 more

in bat I configured the classpath so that the args4j jar in in lib : this are the instructions of bat script

SET JAVA_DIR=C:\Program Files\Java\jdk1.7.0_80\bin\
>CUT
"%JAVA_DIR%\java" -jar ".\lib\buildCSS-1.0.jar"  -cp ".\lib\" -conf "./conf/environment.properties"

I don't understand the deal of java.lang.NoClassDefFoundError . The jar are present and linked by -cp option

Do you have any idea (and solution), please?

You cannot combine -jar and -cp arguments on the command line. If the java command sees -jar it treats everything after the jarfile name as application arguments, AND it ignores any earlier classpath arguments.

You have two choices:

  • Use -cp , include the main JAR in the classpath, and put the full class name for the main class on the command line.
  • Use -jar , and add a "Class-Path" attribute to the main JAR's manifest file listing all of the dependencies.

References:

Note: since you are building the JAR file using Maven, there are other options; for example

  • Use the "Shade" plugin to create an executable "uber-jar" containing all of the dependencies in a single JAR.

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