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

  • Home
  • SEARCH
  • 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 5967317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:55:35+00:00 2026-05-22T19:55:35+00:00

I am trying to run command line commands from Java and a quick sanity

  • 0

I am trying to run command line commands from Java and a quick sanity check made me realize that the reason I am having trouble is that I can’t get the pr.waitFor() call below to work. This programs ends in less than 30s and prints nothing after “foo:”. I expected it to take just over 30 s and print a random number after “foo:”. What am I doing wrong?

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Sample {

    public static void main(String[] args) throws Exception {
        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec("sleep 32; echo $RANDOM");
        pr.waitFor();

        BufferedReader input = new BufferedReader(
                                   new InputStreamReader(pr.getInputStream()));
        String line=null;

        StringBuilder s = new StringBuilder();
        while((line=input.readLine()) != null) {
            System.out.println(s);
            s.append(line);
        }
        System.out.println("foo: " + s.toString());
    }
}
  • 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-22T19:55:36+00:00Added an answer on May 22, 2026 at 7:55 pm

    You should try this:

    Process pr = rt.exec(new String[]{ "bash","-c", "sleep 32; echo $RANDOM" });
    

    This launches a shell process and then within the shell runs your commands. The shell doesn’t return till AFTER the commands have completed.

    Let me know if that works for you.

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

Sidebar

Related Questions

I am trying to run a java program from command line. I tried following
I am trying to run a java based tool using a command line syntax
I'm trying to run .class file from command line. It works when I manually
I'm trying to run DOH from dojo-1.3.2 to test simple Javascript functions from command-line.
I am trying to run a file from command line. The file is a
I am trying to run Selenium tests (PHP) from the command-line. I can only
I am trying to compile and run a program from the command line. When
I'm trying to get my java program to run an svn command from the
I am trying to run the wsdl2java command on a WSDL file that was
I am trying to run a Java program from my Perl script. I would

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.