Solved

how to run java code every day automaticly??

Posted on 2007-04-09
5
534 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 56
hibernate insert example 13 29
Running JavaFX on JDeveloper 12C 1 32
sql import cannot be resolved jsp 3 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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