How to run a Java program under cron and import the jars

泄露秘密 提交于 2019-12-01 08:28:58

问题


My source file is .../MyDir/proj/myProj.java. The jar files are under .../MyDir/proj/library. The jar files are from HTMLUnit 2.10.

This is the source for my cron file:

0 0 * * * java -classpath .../MyDir/proj/ myProj

But it gave me the error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/WebClient

How do I modify the cron file to import the jar files?


回答1:


Something like this:

0 0 * * * java -classpath .../MyDir/proj/:.../MyDir/proj/library/jar1.jar:.../MyDir/proj/library/jar2.jar myProj

or if you are using a recent JVM you can use a wildcard to match all of the JAR files.

0 0 * * * java -classpath .../MyDir/proj/:.../MyDir/proj/library/\* myProj

(The backslash is probably unnecessary because 'globbing' is unlikely to match anything in that context ...)


Better still, put the command (and any others that need to be run to prepare for the launch) into a shell script, and run the script from your crontab entry instead.




回答2:


It is very simple to implement the CRON Job in java

Required Library: quartz-2.0.0.jar

Scheduler Initiator:

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.text.ParseException;

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

public class SchedulerListener{

    static Scheduler scheduler = null;
    public static void main(String[] args) {
        // Setup the Job class and the Job group
        JobDetail job = newJob(FileUploadToAzure.class).withIdentity(
                        "CronQuartzJob", "Group").build();

        // Create a Trigger that fires every hour.
        Trigger trigger;
        try {
            trigger = newTrigger()
            .withIdentity("TriggerName", "Group")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0 * * * ?"))
            .build();

            // Setup the Job and Trigger with Scheduler & schedule jobs
            scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job, trigger);

        } catch (ParseException | SchedulerException e) {
            e.printStackTrace();
        }


    }
} 

Scheduler Class:

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SchedulerJob implements Job {
  @SuppressWarnings("unchecked")
    public void execute(JobExecutionContext context) throws JobExecutionException {
    System.out.println("Print at specific time");
}
}

CronTrigger Expression Meaning 0 0 12 * * ? Fire at 12pm (noon) every day 0 15 10 ? * * Fire at 10:15am every day 0 15 10 * * ? Fire at 10:15am every day 0 15 10 * * ? * Fire at 10:15am every day 0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005 0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day 0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day

More details about CRON Trigger, then please refer below link

http://www.askmani.net/question/crontrigger/




回答3:


For Spring Boot Java users, you can use @scheduled annotation. For more details and a great example, visit this



来源:https://stackoverflow.com/questions/11840059/how-to-run-a-java-program-under-cron-and-import-the-jars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!