Solved

how to run java code every day automaticly??

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
printf performancy 11 86
Html Table Looping (part 2) 5 35
JDBC performance 8 29
Find out all repos that a user is most active on Github 1 33
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

726 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