Solved

how to run java code every day automaticly??

Posted on 2007-04-09
5
517 Views
Last Modified: 2012-05-05
I have a java code and I want it to be run every day automaticly??
what I shout write in the code???
0
Comment
Question by:fm134
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 500 total points
ID: 18874919
Two ways:
1. Use the scheduler provided by OS, i.e. Scheduled Task in Windows, cronjob in Unix etc ( applicable for stand-alone application only)
2. Use a Timer like this (stand-alone application/servlet):

java.util.Timer t = new java.util.Timer();
timer.schedule(new YourTimerTask(), 0, 1000 * 60 * 60 * 24);
// the second parameter is the delay before the first time that runs the code  (in millisecond)
// the last parameter is one day in millisecond

class YourTimerTask extends TimerTask {
      public void run() {
             // code that you want to run evey day
      }
}

0
 

Author Comment

by:fm134
ID: 18874937
tanks pkwan;
so only I have to run  tis code once and it will work every day??
java.util.Timer t = new java.util.Timer();
timer.schedule(new YourTimerTask(), 0, 1000 * 60 * 60 * 24);
// the second parameter is the delay before the first time that runs the code  (in millisecond)
// the last parameter is one day in millisecond

class YourTimerTask extends TimerTask {
      public void run() {
             // code that you want to run evey day
      }
}

0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 18874972
The above timer.schedule will set up a timer that schedules to call your YourTimerTask.run() once a day.
0
 

Author Comment

by:fm134
ID: 18913135
hi pkwan ,
where i put this part
java.util.Timer t = new java.util.Timer();
timer.schedule(new YourTimerTask(), 0, 1000 * 60 * 60 * 24);
// the second parameter is the delay before the first time that runs the code  (in millisecond)
// the last parameter is one day in millisecond
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 18913432
You should put it inside the init() of your servlet class (if you are using servlet) or the main method of your class (if you are using stand-alone application)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
nestparen challenge 4 56
wordlen challenge 3 47
Groovy:unable to resolve class error 2 31
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now