Hot questions for Using Ubuntu in cron

Question:

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 temp.sh 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/temp.sh >> /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.

Thanks


Answer:

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

Question:

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 {
    System.out.println("Started");
    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();

    sc.start();
    sc.scheduleJob(job,t1);
  }

}  

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: " CronSched.java  

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(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.ScheduleBuilder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

What to Do?


Answer:

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

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