繁体   English   中英

为什么带有无限while循环和4分钟睡眠的java代码不在Windows中执行?

[英]Why java code with infinite while loop with 4 minutes of sleep not executing in windows?

我想每 4 分钟检查一次情况。 如果发生指定条件,我想关闭 Firefox 并使用 Windows 7 中的批处理脚本重新启动它。我创建了以下代码的可运行 jar 并使用 javaw 运行它,但即使发生该条件它也不起作用。 相同的代码在 eclipse 中工作。

class WebAlert {
public static void main(String[] args) {
    WebAlert w = new WebAlert();
    while(true) {
        try {
            Thread.sleep(240000);
            w.sendPost();
        }catch(Exception e) {
            System.out.println(e);
        }
    }
}
private void sendPost() {
    if(somecondition){
        ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "restartFirefox.bat");
        File dir = new File("C:\\");
        pb.directory(dir);
        pb.start();
    }
}
}

请将目录设置为批处理文件路径并使用 ProcessBuilder 传递必要的参数。

File dir = new File("C:\\path_to_the_batchfile\\");

ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "Start", "restartFirefox.bat");
pb.directory(dir);
pb.start();

暂无
暂无

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

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