We help IT Professionals succeed at work.

setting a timer in Java

onyourmark
onyourmark asked
on
I have Java code which I would like to run at certain times during the day. I have Windows 2008 Server but I might also be able to do this on a Linux server.
Most of the code is in a file I have attached. I want to add code to read from a database and write to a database (both databases will be remote and won't be on this server).

I am wondering what the best way to have this code run at a certain times. Should I user window's scheduler or chron or is there some way within the java code to have it run at certain times? Also, I can compile this code into a class file. Will the be sufficient to run it or do I have to put it in a jar file? Note, in order for the file to run it needs other classes . These classes are from an open source Java program called Weka. I was going to install it on the server as well. Will this work?

Thanks for you help in advance!
JavaWekaJ48TestTrainPred2.java
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
If you want to have it in pure java you can either use Timer and TimeTask classes
or ExctutoThreadPool. You can also download and use java Quartz library
Awarded 2011
Awarded 2011
Commented:
Howevere if this is something you need to run rather rarely, say a few times a day - I'd prefer to use Windows scheduler on Winodws or a cron job in Unix - you'll not need to have a java process present all the time and have the OS or its functions to take care of the schedule
Awarded 2011
Awarded 2011

Commented:
this is where you canm download Quartz
http://quartz-scheduler.org/
Awarded 2011
Awarded 2011

Commented:
Awarded 2011
Awarded 2011

Commented:

Author

Commented:
So your recommendation is to use scheduler/chron I think.
Awarded 2011
Awarded 2011

Commented:
The issues related to reading/writing database or using third ;party java classes are hardly relevant - you can scheduel your runs for any java program or java process any of these ways
Awarded 2011
Awarded 2011

Commented:
Yes, if this is somethiong noot happening on every minute scale, I'd think that cron or windows scheduler will provide less overhead and will take care of your schedule not worse than from within java

Author

Commented:
Thanks. OK, also what about compiling this code into a class file. Will that be sufficient to run it or do I have to put it in a jar file? Note, in order for the file to run it needs other classes . These classes are from an open source Java program called Weka. I was going to install it on the server as well. Will this work?
Awarded 2011
Awarded 2011
Commented:
I do have many java applications which I run couple of times a day for years both using Winodws scheduler on PC and using cron on Solaris or Linux - and I never had any problems with those
Awarded 2011
Awarded 2011

Commented:
In general this is not important if you run your java program from a collection of classes or you pakage them into a jkar file bfore, the main thing that they should be present in the classpath and classpath should be correct; wheteher they are jarred or not is not relevant

Author

Commented:
So I don't need to do more than make them into class files? Is it hard to get them to run using the scheduler?
Awarded 2011
Awarded 2011
Commented:
Usually if you want to move your program form one computer to another, or to deliver it tyo the users, then jar files are more handy than folders with classes, but form the point of view of running java application it does not matter
Awarded 2011
Awarded 2011

Commented:
Your third partyu classes (iusually packaged into the jar file) need to be accessible in the cvlasspath
of course also

Author

Commented:
Thanks so much!
Awarded 2011
Awarded 2011

Commented:
You are always welcome.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.