1

I am want to distribute a jar script that needs to run periodically. I don't know which OS the end user will have.

Is there a cross OS solution that I can use to run a jar file at a specific time each day.

That is, in windows it adds a scheduled task and a cron task etc.

Any help appreciated !

Samosa
  • 845
  • 7
  • 19

2 Answers2

4

You could use a task scheduling framework written in Java, for guaranteeing portability across OS - because each OS has its own scheduling commands, you won't find the same tools across different platforms.

For instance, take a look at Quartz. But be warned, you'll have to learn how to configure, setup and program tasks with it, IMHO it's not that simple. But then again, that's the price you have to pay for portability.

Yet another option would be to schedule a task using a Timer service (take a look at the tutorial), but for that you'll need a Java EE compliant server. Either way notice that "running a jar" really means executing the main method in the jar file, which is just a call to a Java program.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thanks! I'll look into it. Will it create a scheduled task in windows ? Or does it need to run constantly and will run the jar at specific times ? – Samosa Feb 10 '14 at 04:16
  • It'll store tasks in a data base (or other data stores), you can program it to run basically anything you want with great flexibility, in any platform that's supported by Java. – Óscar López Feb 10 '14 at 04:18
  • Ok gr8 ! But does it need to constantly run in the background ? I suppose it does. – Samosa Feb 10 '14 at 04:21
  • @ZendNoob yep, both alternatives (Quartz, Timer) work as long as there's a running process dispatching the tasks, be it Quartz itself or an application server. Notice that you'll also have to configure an external data source for storing the tasks, for example a data base. As I said before: it's not that simple. – Óscar López Feb 10 '14 at 04:24
1

If you are looking for OS independent scheduler, such a thing does not exist. Quartz is a task scheduler for java environment. For example - you can use quartz to schedule tasks in your java program. You have to write a java program and keep running it all the time for using quartz for your requirement. In other words, you have to start your java program when the OS boots up so that it is ready to run your scheduled job. Too much complexity for the task at hand! You should consider using OS specific scheduler for your need.

RaviH
  • 3,544
  • 2
  • 15
  • 14
  • Ok that makes sense ! But is there a OS dependent solution for all 3 major OS ? i.e Windows, Mac, Linux. – Samosa Feb 10 '14 at 04:23
  • @ZendNoob Unfortunately, no OS independent solution exists. Broadly you need to use 2 different paths - one for windows and another one for u*x (linux, mac or any other unix flavor). From your code call cron to schedule the job on all unix flavors. In windows I believe you can schedule a job by inserting a registry entry. Hold registry entry snippet in your code and use regedit to import it in to registry. Hope these clues help. – RaviH Feb 10 '14 at 04:28