1

I have an executable JAR file on my desktop that I need to run once every three hours. I have no idea how to go about this. Please leave your suggestions/ideas.

TRV
  • 17
  • 1
  • 6

2 Answers2

0

You can use crontab (on Linux, Unix or MacOS) to run the jar at any time. To run the jar every 3 hrs, the cron expression would be

0 */3 * * * (java -jar ~/Documents/App/App.jar)

where ~/Documents/App/App.jar would be the location of the jar

For Windows OS, you may use schtasks. Check this link for the same : https://learn.microsoft.com/en-us/windows/desktop/taskschd/schtasks

You may also use the Task Scheduler of Windows to run the jar. Please check Run a jar file using windows scheduler for the same.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • @TRV For your OS, try this: https://stackoverflow.com/questions/15783553/run-a-jar-file-using-windows-scheduler#26932169 – Madhu Bhat Jul 23 '18 at 07:15
  • Hi @Madhu Bhat, this is quite useful. However, I am not sure if this can be used to directly run a .jar file. I have scheduled it for the next 5 minutes, will update. – TRV Jul 23 '18 at 07:57
  • @TRV it's just a scheduler, so if you specify your java path and java arguments correctly, it should work. Yes please do let me know if it works for you. – Madhu Bhat Jul 23 '18 at 08:39
  • Nope not working. I noticed that I was not prompted to enter the system's Username and Password after step 6. – TRV Jul 23 '18 at 09:09
  • I figured out the issue. Both 'Program Files' and my jar file had space in it, so the argument had to be enclosed in double quotes. Tried it again and it works! Thank you for your help @Madhu Bhat – TRV Jul 23 '18 at 09:40
  • Cheers @TRV. Happy to know that it works for you. I have updated my answer. Please accept it. Thanks :) – Madhu Bhat Jul 23 '18 at 09:42
0

Probably, you can execute below command in command prompt to auto-trigger jar execution:

Schtasks /create /tn JarTask /tr C:\Users\abhinav\Desktop\MyApp.jar /sc hourly /mo 3
Abhinav
  • 530
  • 8
  • 21