Solved

how to run java code every day automaticly??

Posted on 2007-04-09
5
550 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

691 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