有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java检查终端是否打开

我现在正在使用Ubuntu,我能够通过Java代码打开终端。但我的问题是,当我在终端打开后关闭程序时,我会看到一条消息,说我打开的终端已经关闭了

以下是我打开终端的代码:

String command = "gnome-terminal -x bash cd ~/Desktop";
String cmd[] = { "gnome-terminal", "-x", "bash", "-c", "sudo gedit ~/Desktop/wat.txt" };
Runtime rt = Runtime.getRuntime();      
Process pr = rt.exec(cmd);

共 (1) 个答案

  1. # 1 楼答案

    您可以在进程上调用waitFor()。API说:

    causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

    returns the exit value of the process. By convention, 0 indicates normal termination.

    如果你想让你的程序继续运行,那么一定要在单独的线程中调用你的代码