问题描述:

I want to know if is possible to close the current java app util another has done some task, my code is this:

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);

}

The string jardir contains the excecution command that will start the other process that I will be listening, something like this:

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

For now, callJar() opens this process and then close the current until the process that I started has been terminated. In other words, close A until B has been closed.

But what I want to do is to close A until B send a signal (B will continue to exist).

Is there a way to listen for a signal from the process that I started?

网友答案:

After searching for an answer, I finally found a solution, maybe this will work for someone so here is what I did:

Based on this answer and this site, I opted to create a communication between two Java apps using the java.net libraries.

In the process A, I have a method that create a server communication and just waits until it receive a message from process 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;
        }
    }
}

this method will return true if the message has been received, then I can proceed to terminate process A.

In the process B, I have a method that just send a message to a socket when I need it...

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");
    }
}

finally, I just change the callJar method to something like this:

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

I would like to find an easier answer, but for now, this works for me.

相关阅读:
Top