3

我的源文件是.../MyDir/proj/myProj.java. jar 文件位于.../MyDir/proj/library. jar 文件来自HTMLUnit 2.10

这是我的 cron 文件的来源:

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

但它给了我错误:

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

如何修改 cron 文件以导入 jar 文件?

4

3 回答 3

3

在java中实现CRON Job非常简单

所需库: quartz-2.0.0.jar

调度发起者:

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();
        }


    }
} 

调度程序类:

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");
}
}

定时触发

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

有关 CRON 触发器的更多详细信息,请参阅以下链接

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

于 2017-03-24T05:42:24.883 回答
2

像这样的东西:

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

或者,如果您使用的是最近的 JVM,则可以使用通配符来匹配所有 JAR 文件。

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

(反斜杠可能是不必要的,因为 'globbing' 在那种情况下不太可能匹配任何东西......)


更好的是,将命令(以及需要运行以准备启动的任何其他命令)放入 shell 脚本,然后从您的 crontab 条目运行脚本。

于 2012-08-07T06:00:26.430 回答
0

对于 Spring Boot Java 用户,您可以使用@scheduled注解。有关更多详细信息和一个很好的示例,请访问

于 2019-06-03T05:21:22.833 回答