关闭当前 Java 应用,直到另一个应用发出信号

Close current Java app until another send a signal

我想知道是否可以关闭当前的 java 应用程序 util 另一个已经完成了一些任务,我的代码是这样的:

private static void callJar(String jardir) throws IOException, InterruptedException {
    // jardir contains the excecution command 
    Process p = Runtime.getRuntime().exec(jardir);
    synchronized (p) {
        // Here I want to wait for p for a signal but not when p has finished
        // but waitFor() do the second
        p.waitFor();
    }
    // If the other jar is correctly loaded, close this jar
    System.exit(0);
}

字符串 jardir 包含将启动我将要侦听的其他进程的执行命令,如下所示:

jardir = "javaw -jar \path\to\anotherjar.jar"

现在,callJar() 打开这个进程,然后关闭当前进程,直到我启动的进程终止。换句话说,关闭 A 直到 B 关闭。

但是我想做的是关闭A,直到B发出信号(B会继续存在)。

有没有办法监听我启动的进程发出的信号?

在寻找答案后,我终于找到了解决方案,也许这对某些人有用所以我就是这样做的:

基于 this answer and this site,我选择使用 java.net 库在两个 Java 应用程序之间创建通信。

在进程 A 中,我有一个方法可以创建服务器通信并等待它从进程 B 收到消息...

private static boolean listen2ExternalProcess() {
    ServerSocket server = null;
    Socket serverSocked = null;
    String line;
    BufferedReader inputReader = null;
    try {
        server = new ServerSocket(3333);
        serverSocked = server.accept();
        inputReader = new BufferedReader(
            new InputStreamReader(serverSocked.getInputStream()));

        while (true) {
            line = inputReader.readLine();
            log.info("Client says: " + line);
            if (line.equals("Kill yourself :D")) {
                return true;
            }
        }
    } catch (UnknownHostException e) {
        log.error("Don't know about this, " + e);
        return false;
    } catch (IOException e) {
        log.error("Couldn't get IO for the connection, " + e);
        return false;
    } finally {
        try {
            if(serverSocked != null) serverSocked.close();
            if(inputReader != null) inputReader.close();
        } catch (IOException ex) {
            log.error("Couldn't get IO for the connection, " + ex);
            return false;
        }
    }
}

如果已收到消息,此方法将 return 为真,然后我可以继续终止进程 A。

在进程 B 中,我有一个方法可以在需要时向套接字发送消息...

public static void talk2ExternalProcess() {
    Socket socket = null;
    BufferedWriter outputWriter = null;
    try {
        socket = new Socket("localhost", 3333);
        outputWriter = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream()));
    } catch (UnknownHostException e) {
        log.error("Don't know about host: localhost, " + e);
    } catch (IOException e) {
        log.error("Couldn't get IO for the connection to localhost, " + e);
    }

    if (socket != null && outputWriter != null) {
        try {
            outputWriter.write("Kill yourself :D");
        } catch (UnknownHostException e) {
            log.error("Trying to connect to unkown host: " + e);
        } catch (IOException e) {
            log.error("IO Exception: " + e);
        } finally {
            try {
                outputWriter.close();
                socket.close();
            } catch (IOException ex) {
                log.error("IO Exception: " + ex);
            }
        }
    } else {
        log.warn("null socket or outputwriter");
    }
}

最后,我只是将 callJar 方法更改为如下所示:

private static void callJar(String jardir) throws IOException {
    Runtime.getRuntime().exec(jardir);
    if (listen2ExternalProcess()) {
        System.exit(0);
    } else {
        log.warn("Something went wrong...");
    }
}

我想找到一个更简单的答案,但就目前而言,这对我有用。