[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Wait for external program to finish

Posted on 2006-04-14
8
Medium Priority
?
558 Views
Last Modified: 2008-03-10
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
Comment
Question by:qbawler311
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 16453160
You need to do

int returnVal = process.waitFor();
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16453300
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 16453308
In the above code :

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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Webstorm
ID: 16453409
Instead, if you don't want to see output :

OutputStream p_out = null;
OutputStream p_err = null;

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16455441
:-)
0
 

Author Comment

by:qbawler311
ID: 16455490
right back at ya :-)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16455514
:-(
0
 

Author Comment

by:qbawler311
ID: 16455555
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question