0

I have a problem, I tried run a sh file from java code to start a JBoss Server, so when I exec this sh file I want to know when it's started and print out my console. Here is my code:

    public static void runStart() {
        try {
            String command = "./Run.sh";
            String s = get_commandline_results(command);
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("done");
    }

    public static String get_commandline_results(String cmd)
            throws IOException, InterruptedException, IllegalCommandException {

        String result = "";
        Process p = null;
        p = Runtime.getRuntime().exec(String.format("bash -c %s", cmd));
        final ProcessResultReader stderr = new ProcessResultReader(
                p.getErrorStream(), "STDERR");
        final ProcessResultReader stdout = new ProcessResultReader(
                p.getInputStream(), "STDOUT");
        System.out.println("starting...");
        stderr.start();
        stdout.start();
        final int exitValue = p.waitFor();// It was delayed here because sh not complete
        System.out.println("started!");// How to go to here?
        if (exitValue == 0) {
            result = stdout.toString();
        } else {
            result = stderr.toString();
        }
        return result;
    }   
}

class ProcessResultReader extends Thread {
    final InputStream is;
    final String type;
    final StringBuilder sb;

    ProcessResultReader(final InputStream is, String type) {
        this.is = is;
        this.type = type;
        this.sb = new StringBuilder();
    }

    public void run() {
        try {
            final InputStreamReader isr = new InputStreamReader(is);
            final BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                this.sb.append(line).append("\n");
            }
        } catch (final IOException ioe) {
            System.err.println(ioe.getMessage());
            throw new RuntimeException(ioe);
        }
    }

    @Override
    public String toString() {
        return this.sb.toString();
    }
}

Thank for your help!

1 Answers1

2

You have contradicting goals here: You want to continue without waiting for the script to terminate (i.e. you want JBoss running) but at the same time, you want to know the result. Short of building a time machine, this is impossible.

What you can do is you can start the script within another script start.sh:

#!/bin/bash

nohup base -c ./Run.sh > /dev/null &

Note: You can omit bash -c by making Run.sh executable.

This script will start Run.sh as a background process and exit immediately. But if JBoss can't start, you won't get an error since that will happen later.

One solution for this dilemma is a script that reads the log files of JBoss for a couple of seconds or until it sees a line of text that means "JBoss has started successfully".

To do that, use the start script and then in the Java code, start reading the log file of JBoss (you may need to wait for it to show up, first).

For this kind of trick, it's always good when you delete the old log files first, before you try to start JBoss. Otherwise, the Java program might read an old log file and continue while JBoss is still trying to start writing a new log.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thank Aaron Digulla! I think so with your guide I will create another script file ex: start.sh : #.. ./Run.sh > test.txt 2> error.txt Yes, so I must read test file to find "JBoss has started successfully" right? please show me? – Phạm Ngọc Kháng Aug 28 '13 at 15:43
  • Yes, that's what you will have to do. I will not show you anything, though, unless you pay me by the hour :-) We on SO try to make you understand what you do, so you can solve your problems by yourself. We don't write code for you. – Aaron Digulla Aug 28 '13 at 15:50
  • Thanks for shared! I will code by myself thank you very much! – Phạm Ngọc Kháng Aug 28 '13 at 15:54