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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:24:21+00:00 2026-06-01T21:24:21+00:00

I am trying to make a Java GUI for the windows command prompt with

  • 0

I am trying to make a Java GUI for the windows command prompt with some simple commands. I am having some issues with additional input. I can run the “dir” command but when I run the “del” command, I need to give a confirmation, but I cant seem to get it to print out the confirmation message.

public static void main(String args[])
{
    try {
        Process p = Runtime.getRuntime().exec("cmd /c dir");
        read(p);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void read(Process p)
{
    try{
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while((line=input.readLine()) != null) {
            System.out.println(line);
        }   
    }
    catch(IOException e1) { e1.printStackTrace(); }
}

Output:

 Volume in drive E has no label.
 Volume Serial Number is E9ED-7E32

 Directory of E:\Code\workspace\Project

04/11/2012  11:07 PM    <DIR>          .
04/11/2012  11:07 PM    <DIR>          ..
04/11/2012  09:53 PM               301 .classpath
04/11/2012  09:53 PM               383 .project
04/11/2012  09:53 PM    <DIR>          .settings
04/12/2012  12:09 AM    <DIR>          bin
04/12/2012  12:09 AM    <DIR>          src
04/11/2012  11:07 PM    <DIR>          test
               2 File(s)            684 bytes
               6 Dir(s)  429,106,937,856 bytes free

But running this will cause it to hang at line=input.readLine()

public static void main(String args[])
{
    try {
        Process p = Runtime.getRuntime().exec("cmd /c del test");
        read(p);
        OutputStream oStream = p.getOutpuStream();
        BufferedWriter sWriter = new BufferedWriter(newOutputStreamWriter(oStream));
        sWriter.write("y");
        sWriter.newLine();
        oStream.close();
        read(p);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

How do I prevent this hanging? I am also concerned that the confirmation “y” isn’t being sent correctly. I feel like it should be read the output and write the input, but this is the way that I found at multiple sites online. Any help 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-01T21:24:22+00:00Added an answer on June 1, 2026 at 9:24 pm

    When you launch a process Java, you have to read both the error and input streams before they fill up. Its not such a big deal under Linux/OS X, but under Windows the buffers between the JVM and the child process are very small and the program will hang if you don’t start reading right away. It looks like the code is doing that for the input stream, but not the error stream, so if the confirmation from del is coming on the error stream then that would be enough to hang the process.

    There is a way in Java to combine output and error streams. The simplest way is to use ProcessBuilder.redirectErrorStream() when you create your process.

    Another possible problem with the code is the use of readLine(). if a child program sends output with no newline, then the parent program won’t receive it – because the call to readline will just be waiting for an end of line (IIRC del does this). Its good to use the BufferedReader, but it really should be reading one byte/char at a time. Its also more robust if the code does that in a dedicated thread, then it won’t hang if the program sends output while you’re trying to pass input in.

    Also, there’s an option on del to make it not prompt for confirmation. Try del /? from the command prompt.

    Good luck!

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

Sidebar

Related Questions

I'm trying to make an online MMO in Java. It will be a simple
i am trying to make a java application with GUI. i am writing a
I am trying to make a Java web application where users can log into
I'm trying to make a GUI in java, but JMenuBar has been giving me
I'm new at Java GUI and I'm trying to make a program that shows
My question is simple. I'm trying to make a set of java.net.URL s that
I'm trying to make a simple java game -- driving a car through a
I'm trying to make a little java program that executes a system command but
I am trying to make a GUI in Java, using something along these lines:
I am trying to make a Java implementation of the Park-Miller-Carta PRNG random number

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.