0

I'm trying to call a java.jar(it is working by double click) from a java application.And it is working with that code below.The main problem is that when the called jar start to work (a gui application), ı cannot use the my main(another gui application) application.I think it is waiting the exec command's end. How can I manage to run this 2 application properly?

  public void handle(Event arg0) {

            Runtime runTime = Runtime.getRuntime();
            try {
                Process process = runTime
                        .exec("java -jar \"D:\\WORKSPACE\\Deploy\\Binary\\XXXX.jar\"");
                BufferedInputStream inputStream = new BufferedInputStream(process.getInputStream());
                BufferedInputStream errorStream = new BufferedInputStream(process.getErrorStream());

                int n1;
                byte[] c1 = new byte[4096];
                StringBuffer standardOutput = new StringBuffer();
                while ((inputStream.read(c1) != -1)) {

                    standardOutput.append(c1.toString());
                }
                System.out.println("Standard Output: " + standardOutput.toString());

                int n2;
                byte[] c2 = new byte[4096];
                StringBuffer standardError = new StringBuffer();
                while (errorStream.read(c2) != -1) {
                    standardError.append(c2.toString());
                }
                System.out.println("Standard Error: " + standardError.toString());

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
ACrescendo
  • 46
  • 10

1 Answers1

1

The issue is with your stream consuming code...

while ((inputStream.read(c1) != -1)) {
    standardOutput.append(c1.toString());
}
System.out.println("Standard Output: " + standardOutput.toString());

int n2;
byte[] c2 = new byte[4096];
StringBuffer standardError = new StringBuffer();
while (errorStream.read(c2) != -1) {
    standardError.append(c2.toString());
}

While you are trying to read from either stream, you will block the calling thread, which is probably the Event Dispatching Thread.

What you need is two things.

  1. A Thread to handle executing the process and
  2. A Thread to handle reading the streams

For example Printing a Java InputStream from a Process

I would also strongly encourage the use of ProcessBuilder, apart from the fact that it encourages you to use separate strings for each argument you want to pass to the process (making it significantly easier to deal with arguments that use spaces) you can also redirect the error string into in the InputStream, making it even easier to manage...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I will try and write the results .Thank you for now:) – ACrescendo Sep 30 '13 at 07:45
  • The linked example pretty much does what you're asking, it uses `java.exe` to launch a Jar and read it's output... – MadProgrammer Sep 30 '13 at 07:46
  • Yes ı am still reading it but ı actually don't want to read output, making the 2 java programs synchronous will be enough.I couldn't understand ur consumer mechanism and Thread usage. – ACrescendo Sep 30 '13 at 07:52
  • Your code works very nice, and will be very nice for me, if I implement it to my application, too. :D – ACrescendo Sep 30 '13 at 08:32
  • Just remember, even if you don't want to use the output, you should still read it, as some processes will stop until their output buffer is flushed... – MadProgrammer Sep 30 '13 at 09:21
  • By the way , I use javaFX, does SwingWorker work properly on javafx forms? And my another question, at java-to-java applications , this method seems good.But how can I achieve this on c#-to-java or vice versa. – ACrescendo Sep 30 '13 at 11:42
  • I'm not sure about FX, doesn't it have a Platform.invokeLater method? No idea about how to achieve this in c#, but I'd imagine the concept is the same – MadProgrammer Sep 30 '13 at 20:16
  • I simply edited your code and SwingWorker worked with JavaFX.As for C#, its start() command (like exec) isn't crashing the program so it was very easy. Thanks for all – ACrescendo Oct 02 '13 at 06:58