在Java中创建一个新进程,退出当前进程 [英] Create a new process in Java, exit the current process
问题描述
在我的代码中,我想重新启动程序.为此,我在 Windows 中使用了以下代码:
In my code, I want to restart the program. For this i have used the following code in Windows:
if(System.getProperty("os.name").contains("Windows"))
{
//For Windows Builds use this
new ProcessBuilder("java", "Launcher").inheritIO().start();
System.exit(0);
}
对于我使用的 Linux 构建
For Linux Builds I used
else
{
//For Linux/Unix or Mac Builds use this
new ProcessBuilder("/bin/bash", "-c" ,"java Launcher").inheritIO().start();
}
现在,Windows 的实现工作得很好.它开始一个新实例并退出旧实例.但是 Linux 的实现有点奇怪.我添加了 System.exit(0);
认为它会在创建新进程后立即终止当前进程,但它似乎退出了进程本身.我无法在 Linux 中以任何方式重新启动该程序,尽管它在 Windows 中是可行的.
So now, the implementation for Windows works just fine. It begins a new instance and exits the old.
But the Linux implementation is kinda a bit odd. I added System.exit(0);
thinking that it will kill the current process right after creating the new one, but it seemed to exit the process itself. I cannot restart the program in anyway in Linux, although it was doable in Windows.
希望得到帮助和反馈!
[2020 年 7 月 28 日]
[28-July-2020]
所以我确实发现新进程创建了,但是IO没有继承到新会话.我调整了一些代码,现在程序创建了新进程,获得了 IO 控制,并在输入命令后退出.
So I did find that the new process is created, but the IO is not inherited to the new session. I tweaked a bit of code and now the program creates the new process, gets IO control and after entering a command, it exits.
if(System.getProperty("os.name").contains("Windows"))
{
//For Windows Builds use this
new ProcessBuilder("cmd", "/c", "java Launcher").inheritIO().start();
System.exit(0);
}
else
{
//For Linux/Unix or Mac Builds use this
long pid = ProcessHandle.current().pid();
System.out.println(pid);
String a=String.valueOf(pid);
Thread.sleep(10000);
System.out.println(new ProcessBuilder("/bin/bash", "-c", "java Launcher").inheritIO().start());
System.exit(1);
}
没有 System.exit(1);
程序继续使用新创建的进程,但旧进程仍在后台运行.当我试图杀死旧进程时,两个进程都被杀死了.
Without System.exit(1);
the program continues with the newly created process, but with the old process still running in the background. When I try to kill the old process, both the processes are killed.
这是新的屏幕截图,上面指定了代码.https://gofile.io/d/MAYLeJ
Here are the new screenshots, with the code specified above. https://gofile.io/d/MAYLeJ
[2020 年 7 月 29 日]
[29-July-2020]
一直在研究代码为何不起作用.我确实收到了 WSL 未检测到的相同代码的异常!
Been working more on why the code is not working. I did get an exception for the same code, which WSL didnt detect!
推荐答案
更新:我确实找到了正确的答案,可能有点复杂,但我会尽量简化.
Update: I did find the right answer and it might be a bit complex, but I shall try to make it as simple as possible.
在这里,我们将需要 2 个单独的类:1 个类用于监视子进程,1 个类是主进程.
In this, we will require 2 separate classes: 1 class to monitor the child process, 1 class which is the main process.
为了简单起见,我将监控类命名为SessionManager
,主类命名为mainClass
For the sake of simplicity, I shall name the monitoring class as SessionManager
and the main class as mainClass
在 SessionManager 类中,我实现了以下代码.
In the SessionManager class, I've implemented the following code.
try
{
//A loop which will run infinitely to montior and start new processes
while(true)
{
//Create the process and listen to the exit code generated by the child process spawned.
ProcessBuilder session_monitor=new ProcessBuilder("java", "<classname>");
Process process_monitor= session_monitor.inheritIO().start();
//wait for the child process to end before proceeding
process_monitor.waitFor();
switch(process_monitor.exitValue())
{
//Implement the logic here
case 0: //do something
break;
case 1: //do something
break;
}
}
}
catch(Exception E)
{
E.printStackTrace();
}
在mainClass
程序中,我们将有一个main方法来启动进程运行.
And in the mainClass
program, we shall have a main method to start the process running.
class mainClass
{
System.out.println("Hello World");
//exit the process with a code for the SessionManager's logic to work
System.exit(0); //Using a normal exit code here
}
这对我有用,如果您认为可以改进此实现,我很乐意听到.谢谢大家的支持:)
This has worked for me, and if you think this implementation can be improved, I'd love to hear it. Thank you for all your support :)
这篇关于在Java中创建一个新进程,退出当前进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!