简体   繁体   English

为什么Java的自动计划的应用程序无法在Windows上启动?

[英]Why an auto scheduled app from Java didn't start on Windows?

I have a Java Swing app that is supposed to start a 2nd JVM, my code looks like this : 我有一个应该启动第二个JVM的Java Swing应用程序,我的代码如下所示:

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 ? Timer.getTimeFormat没问题,我在看屏幕时已经对其进行了多次测试,App_A启动,到我指定的时间,它将启动第二个JVM,并且App_B像我将启动的那样想看,但是问题是我将其设置为在晚上11点运行,而当我不在屏幕前时,它无法运行,我认为是Windows的睡眠功能导致了问题,我的监视器如果15分钟内没有任何操作,就进入睡眠状态,屏幕变黑,但是我将PC设置为不进入睡眠状态,因此我想知道是否由于显示器的关闭导致我的应用无法按时运行? 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 ? 因为当我早上回来并稍微移动鼠标时,监视器将醒来,并且我可以看到我的App_A仍在屏幕上运行,所以它确实在晚上11点启动了App_B,为什么?

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. 我忘了提一下,我将App_A和App_B震击到一个jar文件中,并将该jar文件放入Windows 7的启动目录中,然后重新启动了PC,在PC重新启动后,我的App_A立即开始运行,但是在监视器转到睡眠,App_A无法在晚上11点准时自动启动App_B。

OK,找到了问题,我还需要将包含所需jar文件的lib目录复制到Startup目录中,以使其正确运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM