NoClassDefFoundError when trying to use outside library in a quartz job Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of NoClassDefFoundError when trying to use outside library in a quartz job without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So I have a perfectly working quartz job as defined below:

package willkara.monkai.jobber;

import java.util.Date;
import org.joda.time.DateTime;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import willkara.monkai.impl.MonKaiClientImpl;


/* this is a test Quartz job to show that we can inject jobs into the jobscheduler from an external location */
public class tester implements Job {

    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("I REALLY HOPE THIS WORKS");
        DateTime dt = new DateTime();

        System.out.println(dt.getDayOfYear());

    }
}

Whenever I run the job in the quartz scheduler on the site, everything runs fine UNTIL it tries to run the JodaTime command.

It outputs the I REALLY HOPE THIS WORKS text, but after that I get this stack trace:

I REALLY HOPE THIS WORKS
2013-11-13 14:31:16,180 ERROR QuartzScheduler_Worker-1 org.quartz.core.JobRunShell - Job DEFAULT.qqq threw an unhandled Exception:
java.lang.NoClassDefFoundError: org/joda/time/DateTime
    at willkara.monkai.jobber.tester.execute(tester.java:16)
    at org.sakaiproject.component.app.scheduler.jobs.SpringJobBeanWrapper.execute(SpringJobBeanWrapper.java:70)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:534)
Caused by: java.lang.ClassNotFoundException: org.joda.time.DateTime
    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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 4 more
2013-11-13 14:31:16,183 ERROR QuartzScheduler_Worker-1 org.quartz.core.ErrorLogger - Job (DEFAULT.qqq threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NoClassDefFoundError: org/joda/time/DateTime]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:534)
Caused by: java.lang.NoClassDefFoundError: org/joda/time/DateTime
    at willkara.monkai.jobber.tester.execute(tester.java:16)
    at org.sakaiproject.component.app.scheduler.jobs.SpringJobBeanWrapper.execute(SpringJobBeanWrapper.java:70)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: org.joda.time.DateTime
    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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 4 more

I am trying to run code from a different external library that I wrote myself. The whole project is managed using maven. I am simply using JodaTime as a test to make sure/see if it will run code from an outside library.

Answer

The problem is that you have compiled your sources with JodaTime libraries in your classpath, but they are not available in target classpath. Ensure they are in your app server’s lib directory or include them in your artifact.

We are here to answer your question about NoClassDefFoundError when trying to use outside library in a quartz job - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji