问题
I have a Java Swing app that is supposed to start a 2nd JVM, my code looks like this :
public class App_A implements Runnable
{
...
public static void start2ndJVM() throws Exception
{
String separator=System.getProperty("file.separator"),classpath=System.getProperty("java.class.path"),path=System.getProperty("java.home")+separator+"bin"+separator+"java";
pb=new ProcessBuilder(path,"-cp",classpath,App_B.class.getName(),"Auto Exit !");
pb.directory(new File("Dir_Data"));
File log=new File("Log.txt");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
}
public void run()
{
try
{
Timer.Start();
while (true)
{
Thread.sleep(60*1000); // Sleep for 1 minute
if (Timer.getTimeFormat(-1,2).equals("23:00")) start2ndJVM(); // 18:28 Run every night at 11 PM to update stock data
}
}
catch (Exception e) { e.printStackTrace(); }
}
...
}
There is no problem with Timer.getTimeFormat, I've tested it many times when I'm looking at the screen, App_A starts, when it comes to the time I specify, it will start the 2nd JVM and the App_B starts as I would like to see, but the problem is when I set it to run at 11 pm, while I was not in front of the screen, it won't run, I think it's the windows' sleep function that is causing the problem, my monitor goes to sleep if there is no action for 15 minutes, the screen goes dark, but I set my PC not to go to sleep, so I wonder could it be the inactivation of monitor caused my app not to run on time ? Because when I came back in the morning and moved the mouse a bit, the monitor will wake up, and I can see my App_A is still running on screen, it just did start the App_B on 11 pm, why ?
I forgot to mention, I jarred the App_A and App_B into a jar file and put that jar file in the Windows 7's Startup dir, and restarted the PC, after the PC restarts, my App_A immediately starts to run, but after the monitor goes to sleep, App_A failed to auto start App_B at 11 pm on time.
回答1:
OK, found the problem, I need to also copy the lib dir that contains a needed jar file into the Startup dir to make it run correctly.
来源:https://stackoverflow.com/questions/22108997/why-an-auto-scheduled-app-from-java-didnt-start-on-windows