简体   繁体   中英

java.lang.NoClassDefFoundError: org/quartz/SchedulerFactory

I'm writing an application using Java Quartz library.

My pom.xml have following dependencies.

<dependencies>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.pranayhere</groupId>
            <artifactId>recurr</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
    </dependencies>

It works when I run this in intellij but doesn't work when I run using java -jar target/application.jar

I get the following error:

java -jar target/test-rrule-quartz-1.0-SNAPSHOT.jar 
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/quartz/SchedulerFactory
        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: org.quartz.SchedulerFactory
        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)
        ... 7 more

Let me know if any extra information is required.

The fact that you've added the dependency in maven only means that while you compile your code the jar of quartz is in the classpath. This means that you can use classes/Interfaces from that jar in your code.

However once the maven compiles your code and the jar is created, its your responsibility to arrange a "runtime" (what you do with java -jar ). That is, you have to add the jar of quartz to the classpath. Maven is a build framework, it has no knowledge about runtime.

There are many ways to solve this issue, to name a few:

  1. Run with -cp flag and add all the jars to the classpath. In this case you'll have to provide a path to all the jars, so probably you'll want to copy all the dependencies into some kind of "lib" folder and distribute with the artifact. This is somewhat an "old-school" way to do this job.

  2. Use "Fat Jar" (see here in case you're not familiar with this concept) which is - "bake" all the code from the dependent jars into one jar that you create ( test-rrule-quartz-1.0-SNAPSHOT.jar in this case). This way the result artifcat will be big but it won't require other dependencies.

  3. More exotic but still can be handy - use spring boot, it already wraps everything into one artifact with the help of a special 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