I have a batch file in my C: drive named abc.bat which I want to execute every two minutes. I have to do this with Java. I need to use the TimerTask class. Below is my current code:
import java.util.TimerTask;
import java.util.Date;
import java.util.Timer;
// Create a class extends with TimerTask
public class ScheduledTask extends TimerTask {
// Add your task here
public void run() {
Runtime.getRuntime().exec("cmd.exe /c start abc.bat");
}
}
//Main class
public class SchedulerMain {
public static void main(String args[]) throws InterruptedException {
Timer time = new Timer(); // Instantiate Timer Object
ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class
time.schedule(task, now ,TimeUnit.SECONDS.toMillis(2));
}
}
If your
abc.batfile is located inc:\then useAlso you need to create
if you want to use it in
time.schedule(task, now ,TimeUnit.SECONDS.toMillis(2));or just use
time.schedule(task, new java.util.Date() ,TimeUnit.SECONDS.toMillis(2));