简体   繁体   中英

Using the command prompt from java

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.

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!

All its standard io (ie stdin, stdout, stderr) operations will be redirected from the original process to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). Can you please check if the confirmation for the message is part of ErrorStream. Also for you to input the confirmation y for delete, you need to use the inputStream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM