0

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?

2
  • ProcessBuilder and ScheduledExucutorService. Commented Aug 19, 2013 at 19:16
  • 1
    It should start 12 hours from when? From when the first script ends? Or from now? What API will you use in the first java program to schedule the second? Commented Aug 19, 2013 at 19:18

3 Answers 3

1

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

}
Sign up to request clarification or add additional context in comments.

4 Comments

I think using ScheduledExecutorService would be better than Timer
I think both approaches require that the first app stay alive for 12 hours. I don't know if there's an alternative.
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)?
Ah.. I didn't know that was a requirement. Use a system call with the at command.
0

Look into Quartz, a Java scheduling library.

2 Comments

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)?
Use the Windows scheduler.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.