1

Probably rather simple question, that I haven't been able to find out how to do. What I want to do is simply restarting my Java application every day at midnight. So I'm imagining something that checks for the current data every minute and if the new date greater than the initial date, then the following is executed:

try {
    Runtime.getRuntime().exec("java -jar fxmonitor.jar");
} catch (IOException e) {
    e.printStackTrace();
}
System.exit(0);

Any hints greatly appreciated.

Left4Cookies
  • 567
  • 2
  • 7
  • 19
  • 5
    This is something you should really do via the regular execution tools your operating system provides (cron on unix or task scheduler on windows). – Philipp May 04 '17 at 07:16
  • 1
    In windows you have Task scheduler that might help . and it will restart the program for you – Rahul Singh May 04 '17 at 07:17
  • 2
    Possible duplicate of [Running an application automatically every day](http://stackoverflow.com/questions/25205685/running-an-application-automatically-every-day) – JJJ May 04 '17 at 07:17
  • I think that you don't have to do it in the code. Create your jar and schedule it's execution on your OS. – Alee May 04 '17 at 07:19
  • Please specify your OS. This kind of thing is usually done with OS tools or third-party tools, not Java code. –  May 04 '17 at 12:52

4 Answers4

1

You could use system supported features like Task scheduler or crontab.

gonczor
  • 3,994
  • 1
  • 21
  • 46
1

I think I have a sample for your issue

class SayHello extends TimerTask {

    public void run() {
      try {
           Runtime.getRuntime().exec("java -jar fxmonitor.jar");
      } catch (IOException e) {
           e.printStackTrace();
      }
    }

}

Call the same from your main method:

public class sample {

    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new SayHello(), 2000, 2000); // replace 2000 with 24hr converted to millisec
    }

}
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
1

MAJOR EDIT: Firstly you need to create a timer. I would suggest creating a public variable Timer midnightTimer = new Timer(). Then create a new class that extends a TimerTask :

private class PeriodicTask extends TimerTask {
    @Override
    public void run() {
        // ... YOUR TASK (code later in this post)
    }
}    

Then meanwhile the program runs you probably want to launch on a Date this following code should get midnight of whatever day it is currently:

// today    
Calendar date = new GregorianCalendar();
// reset hour, minutes, seconds and millis
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);

// next day
date.add(Calendar.DAY_OF_MONTH, 1);
Date midnightTonight = date.getTime();

An example can be seen here java timer task schedule


What you could do is launch your java program using the command line. This can be done through Runtime.getRuntime().exec()

(for more information look at Run .exe file from Java from file location and how to execute command line .exe file in java)

An example of this is:

try {
    Runtime.getRuntime().exec("cmd.exe /c start " + PATHWAYTOYOURPROGRAM); //put here the absolute or relative path to launch the exe
} catch (IOException e) {
    // ... could not launch
}

After you have relaunched your program you could then kill the original program:

System.exit(0);

EDIT: AlexlH has a good point. If you do have a error and it does crash then it will no longer run at midnight. You could also research creating a scheduled task from your java program (if one doesn't already exist) which then runs the java program again at midnight. But that would be harder to do. Here is a link that might help:

Community
  • 1
  • 1
JFreeman
  • 974
  • 1
  • 10
  • 26
  • To be fair, this has one risk. If the app crash, it will not start since the process will not exist. Same problem if you do this using a different process (just to start at specific time), it could crash. In the other hand, OS integrated scheduler are safer (probably not perfect either) but the risk is minimal. – AxelH May 04 '17 at 07:40
  • True but it seemed as if Left4Cookies wanted to do this with code. – JFreeman May 04 '17 at 07:45
  • He probably see this as the only solution. It is working, don't hear me wrong, this just need to point the risk. – AxelH May 04 '17 at 07:55
0

Use Quartz scheduler. From their website:

What is the Quartz Job Scheduling Library?

Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering.

http://www.quartz-scheduler.org/

Gernot
  • 1,094
  • 4
  • 25
  • 39