title: What is the best solution to perform scheduled tasks in Java, whatever the OS?
body: <p>I'd like to generate alarms on my Java desktop application :</p>
  
  <ul>
  <li>alarms set with a specific date/time which can be in 5 minutes or 5 months</li>
  <li>I need to be able to create a SWT application when the alarm is triggered</li>
  <li>I need this to be able to work on any OS. The software users will likely have Windows (90% of them), and the rest Mac OS (including me)</li>
  <li>the software license must allow me to use it in a commercial program, without requiring to open source it (hence, no GPL)</li>
  <li>I cannot require the users to install Cygwin, so the implementation needs to be native to Windows and Unix</li>
  </ul>
  
  <p>I am developing using Java, Eclipse, SWT and my application is deployed from my server using Java Web Start. I'm using Mac OS X.6 for developing.</p>
  
  <hr>
  
  <p>I think I have a few options:</p>
  
  <ol>
  <li>Run my application at startup, and handle everything myself;</li>
  <li>Use a system service.</li>
  <li>Use the cron table on Unix, and Scheduled Tasks on Windows</li>
  </ol>
  
  <hr>
  
  <h1>Run at startup</h1>
  
  <p>I don't really like this solution, I'm hoping for something more elegant.<br>
  Refs: <a href="https://stackoverflow.com/questions/2949649/i-would-like-to-run-my-java-program-on-system-startup-on-mac-os-windows-how-can">I would like to run my Java program on System Startup on Mac OS/Windows. How can I do this?</a></p>
  
  <h1>System service</h1>
  
  <p>If I run it as a system service, I can benefit from this, because the OS will ensure that my software:</p>
  
  <ul>
  <li>is always running</li>
  <li>doesn't have/need a GUI</li>
  <li>restarts on failure</li>
  </ul>
  
  <p>I've researched some resources that I can use:</p>
  
  <ul>
  <li><a href="http://winrun4j.sourceforge.net/" rel="nofollow noreferrer">run4j</a> — CPL — runs on Windows only, seems like a valid candidate</li>
  <li><a href="http://commons.apache.org/daemon/jsvc.html" rel="nofollow noreferrer">jsvc</a> — Apache 2.0 — Unix only, seems like a valid candidate</li>
  <li><a href="http://wrapper.tanukisoftware.com/doc/english/licenseOverview.html" rel="nofollow noreferrer">Java Service Wrapper</a> — Various — I cannot afford paid licenses, and the free one is a GPL. Hence, I don't want to/can't use this</li>
  </ul>
  
  <p>My questions in the system service options are:</p>
  
  <ol>
  <li>Are there other options?</li>
  <li><p>Is my planned implementation correct: </p>
  
  <ul>
  <li>at the application startup, check for existence of the service</li>
  <li>if it is not installed:
  
  <ul>
  <li>escalate the user to install the service (root on Unix, UAC on Windows)</li>
  <li>if the host OS is Windows, use run4j to register the service</li>
  <li>if the host OS is Unix, use jsvc to register the service </li>
  </ul></li>
  <li>if it is not running, start it</li>
  </ul></li>
  </ol>
  
  <p>Thus, at the first run, the application will install the service and start it. When the application closes the service is still running and won't need the application ever again, except if it is unregistered.<br>
  However, I think I still miss the "run on startup" feature.</p>
  
  <p>Am I right? Am I missing something?</p>
  
  <h1>cron / Task Scheduler</h1>
  
  <p>On Unix, I can easily use the cron table without needing the application to escalate the user to root. I don't need to handle restarts, system date changes, etc. Seems nice.</p>
  
  <p>On Windows, I can use the <a href="http://support.microsoft.com/kb/308569" rel="nofollow noreferrer">Task Scheduler</a>, even in command-line using <a href="http://technet.microsoft.com/en-us/library/bb726974.aspx" rel="nofollow noreferrer">At</a> or <a href="http://technet.microsoft.com/en-us/library/cc725744.aspx" rel="nofollow noreferrer">SchTasks</a>. This seems nice, but I need this to be compatible from XP up to 7, and I can't easily test this.</p>
  
  <hr>
  
  <p>So what would you do? Did I miss something? Do you have any advice that could help me pick the best and most elegant solution?</p>