Reputation: 597
Probably rather simple question, that I haven't been able to find out how to do. What I want to do is simply restarting my Java application every day at midnight. So I'm imagining something that checks for the current data every minute and if the new date greater than the initial date, then the following is executed:
try {
Runtime.getRuntime().exec("java -jar fxmonitor.jar");
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
Any hints greatly appreciated.
Upvotes: 1
Views: 2071
Reputation: 1004
MAJOR EDIT: Firstly you need to create a timer. I would suggest creating a public variable Timer midnightTimer = new Timer()
. Then create a new class that extends a TimerTask
:
private class PeriodicTask extends TimerTask {
@Override
public void run() {
// ... YOUR TASK (code later in this post)
}
}
Then meanwhile the program runs you probably want to launch on a Date
this following code should get midnight of whatever day it is currently:
// today
Calendar date = new GregorianCalendar();
// reset hour, minutes, seconds and millis
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
// next day
date.add(Calendar.DAY_OF_MONTH, 1);
Date midnightTonight = date.getTime();
An example can be seen here java timer task schedule
What you could do is launch your java program using the command line. This can be done through Runtime.getRuntime().exec()
(for more information look at Run .exe file from Java from file location and how to execute command line .exe file in java)
An example of this is:
try {
Runtime.getRuntime().exec("cmd.exe /c start " + PATHWAYTOYOURPROGRAM); //put here the absolute or relative path to launch the exe
} catch (IOException e) {
// ... could not launch
}
After you have relaunched your program you could then kill the original program:
System.exit(0);
EDIT: AlexlH has a good point. If you do have a error and it does crash then it will no longer run at midnight. You could also research creating a scheduled task from your java program (if one doesn't already exist) which then runs the java program again at midnight. But that would be harder to do. Here is a link that might help:
Upvotes: 1
Reputation: 2922
I think I have a sample for your issue
class SayHello extends TimerTask {
public void run() {
try {
Runtime.getRuntime().exec("java -jar fxmonitor.jar");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Call the same from your main method:
public class sample {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new SayHello(), 2000, 2000); // replace 2000 with 24hr converted to millisec
}
}
Upvotes: 1
Reputation: 1124
Use Quartz scheduler. From their website:
What is the Quartz Job Scheduling Library?
Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering.
http://www.quartz-scheduler.org/
Upvotes: 0
Reputation: 4146
You could use system supported features like Task scheduler or crontab.
Upvotes: 1