如何在 cron 下运行 Java 程序并导入 jars
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11840059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a Java program under cron and import the jars
提问by S. D. Smith
My source file is .../MyDir/proj/myProj.java
. The jar files are under .../MyDir/proj/library
. The jar files are from HTMLUnit 2.10.
我的源文件是.../MyDir/proj/myProj.java
. jar 文件位于.../MyDir/proj/library
. jar 文件来自HTMLUnit 2.10。
This is the source for my cron file:
这是我的 cron 文件的来源:
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?
如何修改 cron 文件以导入 jar 文件?
采纳答案by Stephen C
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.
或者,如果您使用的是最新的 JVM,您可以使用通配符来匹配所有 JAR 文件。
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.
更好的是,将命令(以及任何其他需要运行以准备启动的命令)放入 shell 脚本中,然后从您的 crontab 条目中运行该脚本。
回答by Manimaran Samuthirapandi
It is very simple to implement the CRON Job in java
在java中实现CRON Job非常简单
Required Library:quartz-2.0.0.jar
所需库: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
有关 CRON Trigger 的更多详细信息,请参阅以下链接