Hot questions for Using Ubuntu in cron


I have created a simple java program for printing "Hello world". code is as follows

public class Hello {

    public static void main(String[] args) {

        System.out.println("Hello world!");


Class has been compiled already and it is in the same directory. In same directory I have created a shell file named which contains command

java Hello

now if I execute this shell file then java program is being executed successfully. I have written a cronjob to execute it every minute

* * * * * /home/ashutosh/Documents/javaPractice/ >> /var/log/myjob.log 2>&1

now in my logs it is showing

could not load main class java 

i don't know what is the problem. I have tried by giving absolute path of the java file but nothing helped. Help me.



Your problem is that the working directory of the cronjob is not the directory where your class resides. So add a cd the_right_directory into the script or change the classpath of java with something like java -cp some_directory Hello


I am crating a cron scheduler for which i am using the following code:-

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.ScheduleBuilder;

public class CronSched {

public static void main(String[] args) throws SchedulerException {
    JobDetail job = JobBuilder.newJob(CronJob.class).build();
    Trigger t1 = TriggerBuilder.newTrigger().withIdentity("MAILER").withSchedule(CronScheduleBuilder.cronSchedule("0 33 2 1/1 * ? *")).build();
    Scheduler sc = StdSchedulerFactory.getDefaultScheduler();



and I am compiling teh following code in terminal with the following statement:-

javac -cp ".:/home/aditya/CronSched/lib/quartz-2.2.3.jar: /home/aditya/CronSched/lib/slf4j-api-1.7.25.jar: /home/aditya/CronSched/lib/slf4j-simple-1.7.25.jar: "  

and on running the code I am using the following statement:-

java CronSched  

and I am getting the following error:-

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/quartz/ScheduleBuilder
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(
at java.lang.Class.privateGetMethodRecursive(
at java.lang.Class.getMethod0(
at java.lang.Class.getMethod(
at sun.launcher.LauncherHelper.validateMainClass(
at sun.launcher.LauncherHelper.checkAndLoadMain(

Caused by: java.lang.ClassNotFoundException: org.quartz.ScheduleBuilder
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
... 7 more

What to Do?


You have to add all necessaries jars to your runtime classpath.

java -cp "quartz-2.2.3.jar:...." CronSched