在Java新建进程,退出当前进程
Create a new process in Java, exit the current process
在我的代码中,我想重新启动程序。为此,我在 Windows:
中使用了以下代码
if(System.getProperty("os.name").contains("Windows"))
{
//For Windows Builds use this
new ProcessBuilder("java", "Launcher").inheritIO().start();
System.exit(0);
}
对于 Linux 我使用的版本
else
{
//For Linux/Unix or Mac Builds use this
new ProcessBuilder("/bin/bash", "-c" ,"java Launcher").inheritIO().start();
}
所以现在,Windows 的实现工作得很好。它开始一个新实例并退出旧实例。
但是 Linux 实现有点奇怪。我添加了 System.exit(0);
以为它会在创建新进程后立即杀死当前进程,但它似乎退出了进程本身。尽管在 Windows.
中可行,但我无法在 Linux 中以任何方式重新启动程序
希望得到帮助和反馈!
编辑:[2020 年 7 月 28 日]
所以我确实发现创建了新的进程,但是IO并没有继承到新的session。我调整了一些代码,现在程序创建新进程,获取 IO 控制并在输入命令后退出。
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);
程序将继续新创建的进程,但旧进程仍在后台 运行。当我尝试终止旧进程时,两个进程都被终止了。
这是新的屏幕截图,上面指定了代码。
https://gofile.io/d/MAYLeJ
编辑:[2020 年 7 月 29 日]
一直在研究为什么代码不起作用。我确实得到了相同代码的异常,WSL 没有检测到!
The Error Log
更新:我确实找到了正确的答案,它可能有点复杂,但我会尽量让它尽可能简单。
在此,我们将需要 2 个单独的 classes:1 个 class 用于监视子进程,1 个 class 是主进程。
为了简单起见,我将监控 class 命名为 SessionManager
,将主要 class 命名为 mainClass
在 SessionManager class 中,我实现了以下代码。
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方法来启动进程运行。
class mainClass
{
public static void main(String[] args)
{
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
}
}
这对我有用,如果您认为可以改进此实现,我很乐意听到。感谢大家的支持:)
在我的代码中,我想重新启动程序。为此,我在 Windows:
中使用了以下代码if(System.getProperty("os.name").contains("Windows"))
{
//For Windows Builds use this
new ProcessBuilder("java", "Launcher").inheritIO().start();
System.exit(0);
}
对于 Linux 我使用的版本
else
{
//For Linux/Unix or Mac Builds use this
new ProcessBuilder("/bin/bash", "-c" ,"java Launcher").inheritIO().start();
}
所以现在,Windows 的实现工作得很好。它开始一个新实例并退出旧实例。
但是 Linux 实现有点奇怪。我添加了 System.exit(0);
以为它会在创建新进程后立即杀死当前进程,但它似乎退出了进程本身。尽管在 Windows.
希望得到帮助和反馈!
编辑:[2020 年 7 月 28 日]
所以我确实发现创建了新的进程,但是IO并没有继承到新的session。我调整了一些代码,现在程序创建新进程,获取 IO 控制并在输入命令后退出。
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);
程序将继续新创建的进程,但旧进程仍在后台 运行。当我尝试终止旧进程时,两个进程都被终止了。
这是新的屏幕截图,上面指定了代码。 https://gofile.io/d/MAYLeJ
编辑:[2020 年 7 月 29 日]
一直在研究为什么代码不起作用。我确实得到了相同代码的异常,WSL 没有检测到!
The Error Log
更新:我确实找到了正确的答案,它可能有点复杂,但我会尽量让它尽可能简单。
在此,我们将需要 2 个单独的 classes:1 个 class 用于监视子进程,1 个 class 是主进程。
为了简单起见,我将监控 class 命名为 SessionManager
,将主要 class 命名为 mainClass
在 SessionManager class 中,我实现了以下代码。
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方法来启动进程运行。
class mainClass
{
public static void main(String[] args)
{
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
}
}
这对我有用,如果您认为可以改进此实现,我很乐意听到。感谢大家的支持:)