• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

Wait for external program to finish

I'm doing this to run a program from the windows cmd in java:
 
    String[] com = {"cmd.exe", "/C", "Start", "runThis.exe"};
    Runtime.getRuntime().exec.(com);
 
The problem:  I need "runThis.exe" to finish running before my java code continues.  As it is now the code continues to run as soon as it launches runThis.exe.
0
qbawler311
Asked:
qbawler311
  • 4
  • 2
  • 2
1 Solution
 
CEHJCommented:
You need to do

int returnVal = process.waitFor();
0
 
WebstormCommented:
You may also need to read process output (stdout & stderr) :

        Runtime.getRuntime().exec.(com);

        Thread th_out,th_err;
        // Process. OUT
        (th_out=new Thread(){
            byte b[]=new byte[8192];
            InputStream inp=p.getInputStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=inp.read(b);
                        if (p_out!=null) p_out.write(b,0,sz);
                    }
                }catch(Exception e){}
            }
        }).start();

        // Process. ERR
        (th_err=new Thread(){
            byte b[]=new byte[8192];
            InputStream inp=p.getErrorStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=inp.read(b);
                        if (p_err!=null) p_err.write(b,0,sz);
                    }
                }catch(Exception e){}
            }
        }).start();

        int returnVal = process.waitFor();

        th_out.interrupt();
        th_err.interrupt();
0
 
WebstormCommented:
In the above code :

OutputStream p_out = System.out;
OutputStream p_err = System.err;

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
WebstormCommented:
Instead, if you don't want to see output :

OutputStream p_out = null;
OutputStream p_err = null;

0
 
CEHJCommented:
:-)
0
 
qbawler311Author Commented:
right back at ya :-)
0
 
WebstormCommented:
:-(
0
 
qbawler311Author Commented:
sorry webstorm but i thought it would be infair to split points since CEHJ answered the question completely first.  Thanks for the extra info though. :-/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now