As part of automation I want to schedule a java program after 12 hours using another java program that is currently running. My client machine is windows. I can't say when my first script will start and once it ends, it has to schedule the second script which should start after 12 hours. Any suggestions on how to do it?
3 Answers
I would use java.util.Timer.schedule(TimerTask task, long delay). The task that you schedule can then invoke the second java program appropriately. For example:
public void scheduleTask() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
try {
Runtime.getRuntime().exec("java secondprog.class &");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 12*1000*60*60);
}
4 Comments
jimSampica
I think using
ScheduledExecutorService would be better than TimerEdward Falk
I think both approaches require that the first app stay alive for 12 hours. I don't know if there's an alternative.
Leo
All the approaches requires the first program to be alive.Is there a way to schedule a bat file in windows using java(using cli in windows)?
Trenin
Ah.. I didn't know that was a requirement. Use a system call with the
at command.You can use the Windows at command to schedule a task to run. This can be done via a system call.
This has already been answered here: https://stackoverflow.com/a/3397348/2471910
ProcessBuilderandScheduledExucutorService.