简体   繁体   中英

Incorrect classpath causing java.lang.NoClassDefFoundError

When I run my java application from the command line I get the following exception:

java -jar target/my-app.jar

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/my_company/utilities/http_server/HttpServer
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.my_company.utilities.http_server.HttpServer
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

Looking into this I have discovered the following:

My local repo contains:

~/.m2/repository/com/my_company/utilities/http_server/0.0.1-SNAPSHOT/utilities-0.0.1-20151208.162219-2.jar

MANIFEST.MF contains the following entry in the classpath:

Class-Path: lib/utilities-0.0.1-20151208.162219-2.jar 

and the target directory contains a lib subfolder with the following file:

utilities-0.0.1-SNAPSHOT.jar

My POM file contains the following entry:

<dependency>
    <groupId>com.my_company.utilities</groupId>
    <artifactId>utilities</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

I can see that the exception is caused by the wrong entry in the classpath. But I don't know why that is wrong - ie why are the names different. Can someone help?

The exception does not occur if I run the application from within Eclipse.

Try disabling the <useUniqueVersions> configuration element:

      <plugin>
         <artifactId>maven-jar-plugin</artifactId>
         <configuration>
           <archive>
             <manifest>
               <addClasspath>true</addClasspath>
               <useUniqueVersions>false</useUniqueVersions>
             </manifest>
           </archive>
         </configuration>
      </plugin>

From Handling Snapshot Versions

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