-6

I am using Runtime.getRuntime().exec function to launch independent GUI Java application for subroutine task.

The code used is in simple manner:

Runtime.getRuntime().exec("java -jar /home/user/jar.jar");

Executing the code doesn't cause any process launch nor error occured! ProcessBuilder has same effect. Checked to work correctly on Windows. As seems, on some platforms it is ignored on system level outside Java, as JRE does not return any kind of error.

EDT: I edited the code to read stderr and stdout by parallel thread to preserve main app execution:

Process p = Runtime.getRuntime().exec(runCmd);
new DaemonFailPrint(p).start();

Thread code is:

public class DaemonFailPrint extends Thread {

    private Process process;

    public DaemonFailPrint(Process process) {
        this.process = process;
    }

    @Override
    public void run() {
        try {
            process.waitFor();

            String out = "";

            while (process.getInputStream().available() > 0) {
                out += (char) process.getInputStream().read();
            }

            out += System.lineSeparator();

            while (process.getInputStream().available() > 0) {
                out += (char) process.getErrorStream().read();
            }

            JOptionPane.showMessageDialog(null, out);
        } catch (InterruptedException | IOException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }

    }

}

The result is: I got empty message box straight after subprocess is "launched".

The mean is Process object seems to be created and finished in same time, but no error out exists.

LifeOnNet
  • 107
  • 2
  • 13
  • Writing a good question takes practice and effort. Please go through the [tour] and do take a look at the [help] section as well as the [how to ask good questions](http://stackoverflow.com/help/how-to-ask) section so hopefully your future questions will be better. – Hovercraft Full Of Eels Dec 19 '15 at 22:30
  • How do you know that nothing has been launched or no errors have occurred? Where do you show us how you handle the Process's InputStream or its error stream? Without these you will not know if the Process is passing any output to its standard out or standard error. – Hovercraft Full Of Eels Dec 19 '15 at 22:59
  • Btw. are you using the "true" Oracle Java on the OpenSuse (which needs to be installed manually because of license issues) or the Java machine distributed with the OpenSuse - OpenJDK? OpenJDK is an alternative implementation which might behave differently in some cases (I already hit some incompatibilities of OpenJDK in the past). – EmDroid Dec 19 '15 at 23:06
  • Nope. I'm aware of casualties of using OpenJDK instead. I run official Oracle distribution installed manually and last updates. – LifeOnNet Dec 21 '15 at 20:43

2 Answers2

0

The Runtime::exec() does not wait for the process to exit, so if you want to detect errors in the executed program itself, you'd need to use something like:

Process process = Runtime.getRuntime().exec("java -jar /home/user/jar.jar");
int rc = process.waitFor();
if (rc != 0)
{
    System.err.println("The process failed with error: " + rc);
}

It might be, that the jar is not found or cannot be executed etc., those errors you normally see on the console, but if you have no console, the only clue might be the return code.

You might also want to check here how to capture the output console: Capturing stdout when calling Runtime.exec

Seems that you can use process.getInputStream() to connect to the output stream of the process. So you can simply copy it to the console to see what happened.

Community
  • 1
  • 1
EmDroid
  • 5,918
  • 18
  • 18
  • I don't try to wait for process to exit, I launch long-playing functionality independent process which, as i can see, is not launched at all, same code IS working on Windows platform – LifeOnNet Dec 19 '15 at 22:35
  • Then you only can see errors of launching the process itself (in your case, java.exe) but not the actual application launched (so you will not be able catch the errors when for example the java.exe cannot find the jar and terminates with error - the java.exe itself will be launched in that case so the process will be created, but it will immediately terminate with an error). Try to launch the same command from the command line, does it work in that case or not? – EmDroid Dec 19 '15 at 22:42
  • It does. Once more, the problem looks like something prevent from starting subprocess by VM with no error on this platform. – LifeOnNet Dec 19 '15 at 22:46
  • Now also tried to read from stderr of subprocess, no errors from java, practically there is nothing to read from both stderr and stdout of subprocess. – LifeOnNet Dec 19 '15 at 22:49
  • Did you also try with process.waitFor()? As if it "doesn't start" the process terminates soon anyway - just to get the clue (and then comment it out afterwards) just to see if there really is some issue like that or not. If you test immediately before waitFor(), the output might not be ready yet. – EmDroid Dec 19 '15 at 22:50
  • I'll try. For now I see it all works then running main process from Netbeans. The subprocess created and working well. – LifeOnNet Dec 19 '15 at 22:53
0

So far so good! I found the answer recently by myself, still don't have a reason why it works this way, but I suppose it's all about internal difference of handling new processes in VM's on different platforms. I had to edit the code this way, and now it works:

String[] runcmd = {"java","-jar","/home/user/jar.jar"};
Runtime.getRuntime().exec(runcmd);

Now it seems to work perfect. As I see it fails to process the file and execute then command with parameters given as same string while no error thrown on Java code level, it's possibly lost in VM internals.

LifeOnNet
  • 107
  • 2
  • 13