I'm trying to make code that opens hourly programs made in Java. I have little knowledge of java
. I tried to do similar below but it doesn't work as I want.
Code:
public static void main(String[] args) throws Exception {
for(int i = 0; i < 5; i ) {
Runtime runtime = Runtime.getRuntime();
String[] s = new String[] {"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"};
Process process = runtime.exec(s);
}
}
CodePudding user response:
If you are looking for running this program in a scheduled manner, you can use something like Task Scheduler in windows or Crontab in case of UNIX systems.
You need not install and run Java for that. But, if you really need it to be executed using a java code, then you can use inbuilt scheduling options in Java. One of the approach is to use a TimerTask
. Added an example below
public class Task extends TimerTask {
@Override
public void run() {
try {
// I don't know, what is this app, basically you execute the logic here
Runtime runtime = Runtime.getRuntime();
String[] s = new String[] { "C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe" };
Process process = runtime.exec(s);
} catch (IOException e) {
// Do your thing with the errors!
e.printStackTrace();
}
}
}
And your scheduler goes like this.
public class Scheduler {
public static void main(String[] args) {
//Create a timer for scheduling
Timer schduleManager = new Timer();
//Create your task instance
Task taskInstance = new Task();
//Scheduler your task repeatedly - every one hour
schduleManager.schedule(taskInstance, 0, TimeUnit.HOURS.toMillis(1));
System.out.println(TimeUnit.HOURS.toMillis(1));
// Keep your code running - an eg.
while(true);
}
}
The program has to be exited forcefully with a Cntrl C
or console kill. There are other similar options , using different libraries as well, like
- java.util.concurrent.ScheduledExecutorService
- Quartz Scheduler
And more. You can explore on this.
CodePudding user response:
If you are using spring boot then you can use Scheduler annotation
@Scheduled(fixedDelay = 60 * 60 * 1000
CodePudding user response:
This seems to be a job requiring Java's ScheduledExecutorService. According to Java's documentation
The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with schedule, which executes a Runnable or Callable task after a specified delay. In addition, the interface defines scheduleAtFixedRate and scheduleWithFixedDelay, which executes specified tasks repeatedly, at defined intervals.
Here's an example from its Javadoc that sets up a ScheduledExecutorService to beep every ten seconds for an hour:
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
Here is a link to a StackOverflow answer that uses ScheduledExecutorService to run a task at a specific time.
Another alternative is to run your task as a CRON job. Here's a link to a StackOverflow answer that uses CRON to schedule a Java program.
CodePudding user response:
If I understood your question correctly you can use Thread.sleep() to idle and then start the Process.
So I guess something like this would work:
public static void main(String[] args) throws InterruptedException, IOException {
ProcessBuilder pb = new ProcessBuilder("path\\to\\file.exe");
while (true) {
pb.start();
Thread.sleep(Duration.ofHours(1).toMillis());
}
}