Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关闭当前Java应用程序,直到另一个应用程序发送信号_Java_Jar_Process - Fatal编程技术网

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

关闭当前Java应用程序,直到另一个应用程序发送信号,java,jar,process,Java,Jar,Process,我想知道是否有可能关闭当前的java应用程序。如果另一个用户完成了一些任务,我的代码如下: private static void callJar(String jardir) throws IOException, InterruptedException { // jardir contains the excecution command Process p = Runtime.getRuntime().exec(jardir); synchronized (p)

我想知道是否有可能关闭当前的java应用程序。如果另一个用户完成了一些任务,我的代码如下:

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
包含EXECUTION命令,该命令将启动我将要侦听的另一个进程,如下所示:

jardir = "javaw -jar \\path\\to\\anotherjar.jar"
private static void callJar(String jardir) throws IOException {
    Runtime.getRuntime().exec(jardir);
    if (listen2ExternalProcess()) {
        System.exit(0);
    } else {
        log.warn("Something went wrong...");
    }
}
现在,
callJar()
打开此进程,然后关闭当前进程,直到我启动的进程终止。换句话说,关闭A直到关闭B为止

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


有没有办法从我开始的过程中听到信号?

在寻找答案后,我终于找到了一个解决方案,也许这对某些人有用,所以我做了以下工作:

基于和,我选择使用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;
        }
    }
}
如果收到消息,此方法将返回true,然后我可以继续终止进程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
方法更改为如下内容:

jardir = "javaw -jar \\path\\to\\anotherjar.jar"
private static void callJar(String jardir) throws IOException {
    Runtime.getRuntime().exec(jardir);
    if (listen2ExternalProcess()) {
        System.exit(0);
    } else {
        log.warn("Something went wrong...");
    }
}

我想找到一个更简单的答案,但现在,这对我来说是可行的。

您看过Java的并发API吗@如果我错了,Chris会纠正我,但这只适用于在一个jar中通信多个线程,不是吗?在我的例子中,我有两个独立的jar在运行,我想传达两者。你能定义“jar”吗?我不认为你所描述的是大多数人读“jar”时会想到的。你可能指的是“过程”。我想正如你所说,它们是两个不同的过程。我之所以称之为“jar”,是因为我打开了它,但它们运行在两个不同的虚拟机中。。。