3

I am using a java program which sends email after finishing up some file transfers.I am using Eclipse to code up the program. How do I set up a cron job to execute this java program for a particular time. Also I have various jar files inside the project. Please suggest

5
  • 2
    How is the fact that you're using eclipse and the fact that you have several jars relevant? If you know how to launch your application, using java -cp ... your.full.ClassName, then just setup a cron job like you would do for any other process.
    – JB Nizet
    Oct 21, 2011 at 21:51
  • Do you mean a job in a continously running program or start up the program using a OS cron job?
    – Thomas
    Oct 21, 2011 at 21:52
  • 2
    @JB it's relevant if he doesn't know how to run his program putting jars on the classpath without clicking the "run" button in eclipse...
    – corsiKa
    Oct 21, 2011 at 21:53
  • As @JBNizet points out, the issue here is to know how to run a Java program from the command line.
    – leonbloy
    Oct 21, 2011 at 21:54
  • OK. I can agree with that. Then why doesn't he ask : how to launch a Java program? Anyway (for the OP), read up about the basics : download.oracle.com/javase/tutorial/getStarted/index.html and download.oracle.com/javase/7/docs/technotes/tools/windows/…
    – JB Nizet
    Oct 21, 2011 at 21:57

4 Answers 4

9
  • Write a shell script to invoke your java program with the necessary arguments.
  • Make sure that the classpath argument points to the jars that you need.
  • Make sure that the shell script has necessary unix permissions.
  • Schedule the script to be invoked by setting up a cron job.

For more info about cronjob look here http://en.wikipedia.org/wiki/Cron

just my 2 cents...

7

r0ast3d has a quick, clear answer - I did have to do some more searching to get each step done so I'll elaborate on his steps:

  1. Write a shell script to invoke your java program with the necessary arguments. Example:

    !/bin/bash
    echo "Running script."
    cd ~/your/classpath/to/java
    java -classpath .:somejar.jar path/to/your/Program
    

    Separate your necessary classpaths with colons (:) rather than semicolons (;) The path to your program should start with your package (find this at the top of the java program)

  2. Make sure that the classpath argument points to the jars that you need. You can check your import statements in your java program to make sure you are specifying all the necessary classpaths. You have to run this script from your java directory, and can use a single period (.) as your first classpath argument.

  3. Make sure that the shell script has necessary unix permissions.

    Run from a terminal: sudo chmod ### yourScript.sh

    Where ### are numbers representing the correct permissions for your system setup.

  4. Schedule the script to be invoked by setting up a cron job.

    Run from a terminal: crontab -e

    This will open your crontab editor. You can add a job in this way:

    */5 * * * * bash /home/scripts/yourScript.sh

    Replace the path to the script with the correct location of your script. This job is set to run every 5 minutes. See http://www.adminschoice.com/crontab-quick-reference/ for a good reference on crontab.

Hope this helps someone out!

4

use quartz for more complex need or Timer for a simpler task

2

There is the cron4j library http://www.sauronsoftware.it/projects/cron4j/. I have used it before to schedule a java program to run weekly. The scheduling syntax is the same as a crontab. The thing is, it needs to be constantly running as a background process to work. I ended up just using normal cron, but it could be useful if you're not on an Unix-like system, and you don't have cron.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.