简体   繁体   中英

NoClassDefFoundError occurs even if dependency was added to pom.xml

So I would like to build my maven project with

mvn clean package

But once I try to run my built jar file in target folder like

java -jar app-1.0-SNAPSHOT.jar

It shows error:

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: net/sourceforge/jdatepicker/DateModel 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: net.sourceforge.jdatepicker.DateModel 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

Pom DataPicker dependency:

<!-- https://mvnrepository.com/artifact/net.sourceforge.jdatepicker/jdatepicker -->
        <dependency>
            <groupId>net.sourceforge.jdatepicker</groupId>
            <artifactId>jdatepicker</artifactId>
            <version>1.3.2</version>
        </dependency>

Why even though dependency was added, exception occurs?

You need to ensure all your dependencies are available to java runtime by adding them to your classpath. Adding a dependency in pom will help only in compiling and packaging the project.

try delete and redownload the jar in your local maven repo.

If your are using idea, try update local repo in the maven setting.

try adding maven plugin so that it copies all your dependencies with the jar file and it does not have to look any further. Do this by adding:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.4</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals><goal>copy-dependencies</goal></goals>
            </execution>
        </executions>
    </plugin>

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