Solution to run a jar file at specific time each d

2019-06-07 00:51发布

问题:

I am want to distribute a jar script that needs to run periodically. I don't know which OS the end user will have.

Is there a cross OS solution that I can use to run a jar file at a specific time each day.

That is, in windows it adds a scheduled task and a cron task etc.

Any help appreciated !

回答1:

You could use a task scheduling framework written in Java, for guaranteeing portability across OS - because each OS has its own scheduling commands, you won't find the same tools across different platforms.

For instance, take a look at Quartz. But be warned, you'll have to learn how to configure, setup and program tasks with it, IMHO it's not that simple. But then again, that's the price you have to pay for portability.

Yet another option would be to schedule a task using a Timer service (take a look at the tutorial), but for that you'll need a Java EE compliant server. Either way notice that "running a jar" really means executing the main method in the jar file, which is just a call to a Java program.



回答2:

If you are looking for OS independent scheduler, such a thing does not exist. Quartz is a task scheduler for java environment. For example - you can use quartz to schedule tasks in your java program. You have to write a java program and keep running it all the time for using quartz for your requirement. In other words, you have to start your java program when the OS boots up so that it is ready to run your scheduled job. Too much complexity for the task at hand! You should consider using OS specific scheduler for your need.