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 8796505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:38:20+00:00 2026-06-13T23:38:20+00:00

I am writing few programs to invoke cmd.exe with additional commands and collect the

  • 0

I am writing few programs to invoke cmd.exe with additional commands and collect the output generated from the same using java. Below is a sample program–

public class LoadShell {
    public static void main(String[] args) throws Exception {
    //Line1
        String [] command ={"cmd.exe","/c","dir"};      
    ProcessBuilder probuilder = new ProcessBuilder( command );             
        Process process = probuilder.start();       
        InputStream inputstream1 = process.getInputStream();        
        InputStreamReader inputstreamreader1 = new InputStreamReader(inputstream1);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader1);      
        String line="";
        while ((line = bufferedreader.readLine()) != null) {
            System.out.println(line);
        }

  }
}

These programs are working fine as expected. Now I have a simpler requirement. I want to invoke cmd.exe without any arguments and collect the output as String. This is because I want to send the process object reference and string to another method for some project specific purpose. So I have modified my code in Line1 as below–

public class LoadShell {
    public static void main(String[] args) throws Exception {
          //Line1
      String [] command ={"cmd.exe"};
      ProcessBuilder probuilder = new ProcessBuilder( command );             
          Process process = probuilder.start(); 
          InputStream inputstream1 = process.getInputStream();        
          InputStreamReader inputstreamreader1 = new InputStreamReader(inputstream1);
          BufferedReader bufferedreader = new BufferedReader(inputstreamreader1);
          int intch;     
        String line="";
        while ((line = bufferedreader.readLine()) != null) {
            System.out.println(line);
        }

       }
}

But in this case, the readLine method hangs indefinitely after printing–

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

So I have also tried using read of BufferedReader instead of readline as follows —

while ((intch = bufferedreader.read()) != -1) {
            int ch = (char) intch;
            System.out.println(ch);
          }

But even read is hanging after reading the bytes corresponding to —

 Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

Is there any way I can get the complete output that is below without causing my program to hang —

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\devshankhasharm>

Any ideas would be appreciated.

  • 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-06-13T23:38:21+00:00Added an answer on June 13, 2026 at 11:38 pm

    Probably because without the /c flag you’re simply opening a shell window, and that external process is now sitting at the C:\> prompt waiting for a command to be issued. Since you never send anything, it’ll sit there until you kill it.

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

Sidebar

Related Questions

I'm writing some code which involves running a few shell commands from Python and
I am new to perl and writing my first few programs and using its
I am new to lisp and am writing a few simple programs to get
I am writing a library which uses a few functions from the windows user32.dll
So far I have been writing programs in Java. So when I started C++,
I've been tasked with writing a few custom C# reporting programs for a client.
I am writing JVMTI code to profile Java programs, which mostly entails obtaining stack
I'm writing a program in C# that will need to store a few Data
I am writing a program that is cross platform. There are a few spots
So I'm tasked with writing a few SQL queries that involve finding the highest

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.