我认为它应该多次打印邮件,但只打印一次?有什么问题?感谢。
import java.io.IOException;
public class RestartApplication {
public static void main(String[] args) {
System.out.println("Test restarting the application!");
restart();
}
private static void restart() {
try{
Runtime.getRuntime().exec("java RestartApplication");
}catch(IOException ie){
ie.printStackTrace();
}
}
}
答案 0 :(得分:2)
它只打印一次的原因是你需要打印进程的输出,否则它将以静默方式运行:
Process process = Runtime.getRuntime().exec("java RestartApplication no-run");
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
当显示输出时,您将看到一系列进程,每个进程都会开始RestartApplication
的新副本,这将消耗大量资源,因此您可能希望考虑传入命令行参数 not not 开始另一个过程。
即使是简单的参数检查也会通过将进程数限制为2来保存您的系统:
if (args.length == 0) {
restart();
}
答案 1 :(得分:1)
我怀疑运行它在命令行上不起作用,所以当你从Java运行它时它不会工作。
System.out.println("Test restarting the application!");
Process exec = Runtime.getRuntime().exec(new String[]{"java", "-cp", System.getProperty("java.class.path"), "RestartApplication"});
BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
for (String line; (line = br.readLine()) != null; )
System.out.println(line);
打印
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!
Test restarting the application!