Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 401933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:04:44+00:00 2026-05-12T17:04:44+00:00

I have a Java program that executes specific commands into the OS. Am also

  • 0

I have a Java program that executes specific commands into the OS. Am also using Process.waitfor() as showing in the code below to indicates if the execution completed successfully or failed.

My question is, is there any other way to avoid using process.waitfor(), Is there a way to use while loop and perform certain action until the process is completed?

            Runtime rt = Runtime.getRuntime();

        Process p = rt.exec(cmdFull);

        BufferedReader inStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inStreamLine = null;
        String inStreamLinebyLine=null;
        while((inStreamLine = inStream.readLine()) == null) {
          inStreamLinebyLine = inStreamLinebyLine+"\n"+inStreamLine;
        }


        try {
            rc = p.waitFor();

        } catch (InterruptedException intexc) {
            System.out.println("Interrupted Exception on waitFor: " +
                               intexc.getMessage());
        }    

Waht I wish to do, is something like this

            Runtime rt = Runtime.getRuntime();

        Process p = rt.exec(cmdFull);

        BufferedReader inStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inStreamLine = null;
        String inStreamLinebyLine=null;
        while((inStreamLine = inStream.readLine()) == null) {
          inStreamLinebyLine = inStreamLinebyLine+"\n"+inStreamLine;
        }


        try {

            while ((rc = p.waitFor()) == true ) { // This is made up, I don't even think it would work
                System.out.println('Process is going on...');
            }


        } catch (InterruptedException intexc) {
            System.out.println("Interrupted Exception on waitFor: " +
                               intexc.getMessage());
        }    

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:04:44+00:00Added an answer on May 12, 2026 at 5:04 pm

    Maybe something like this would work. Create a Thread as @tschaible suggested, then do a join on a that thread with a timeout (which is the part you made up in your code). It would look something like this:

    Thread t = new Thread(new Runnable() { 
    
      public void run() {
        // stuff your code here
      }
    
    });
    t.run();
    
    while (t.isAlive()) {
      t.join(1000); // wait for one second
      System.out.println("still waiting");
    }
    

    What this does is launch the code as a separate thread and then test if the tread finished every one second. The while loop should end when thread finishes and is no longer alive. You might have to check for InterruptedException but I can’t test that now.

    Hope this sends you in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that currently executes many external programs using java.lang.Process . Assuming
I have a Java program that executes from Spring Qquartz every 20 seconds. Sometimes
I have a Java program that opens a file using the RandomAccessFile class. I'd
I have a Java program that connects to a webserver using SSL/TLS, and sends
I have a Java program that executes Runtime.getRuntime().exec(ls -l); many times, once for each
I have a Java program that executes sh in interactive mode as a subprocess.
I have a java program that performs 5 different tasks. When I run the
I have a java program that is running in the background(Windows). I would like
I have a Java program that runs many small simulations. It runs a genetic
I have a java program that will work with a variety of Java Beans.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.