Home > Mobile >  ProcessBuilder execute command on cmd
ProcessBuilder execute command on cmd

Time:10-23

I facing problem on cmd Process i can't execute command on cmd and retrieve the result and print it. anyone can help me ?

public class rev {
    public static void main(String args[]) throws InterruptedException, IOException {
        String host="127.0.0.1";
        int port=4444;
        String cmd="cmd.exe",readLine;
        BufferedWriter writer;
        BufferedReader read;
        
        
        Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();
        Socket s=new Socket(host,port);
        
        System.out.print(p.getInputStream().read());
        writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        writer.write("dir");
        read = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while((readLine = read.readLine()) != null) {
            System.out.print(read.readLine());
        }
    }
}

CodePudding user response:

You invoke the ProcessBuilder.start() method. The method returns a Process instance, and in it's documentation you can read

By default, the created process does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the process. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the process may cause the process to block, or even deadlock.

So it is likely your process is started by the OS but gets blocked due to I/O restrictions. Get around that by reading the STDOUT and STDERR streams until your process finishes. Or redirect the streams, like so:

ProcessBuilder pb = new ProcessBuilder("cmd.exe");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
p.waitFor();
System.out.println("process exited with "   p.exitValue());

CodePudding user response:

You have not sent system line separator to terminate the line for cmd.exe. This demonstrates one way to set up the command:

public static void main(String ... args) throws IOException, InterruptedException {
    String cmd="cmd.exe";

    Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();

    try(Writer stdin = new OutputStreamWriter(p.getOutputStream())) {
        stdin.write("dir" System.lineSeparator());
    }
    p.getInputStream().transferTo(System.out);
    int rc = p.waitFor();
    System.out.println("waitFor()=" rc);
}

Note that the above is a demonstration. If you plan to send many commands to the STDIN of sub-process you will need to stdin.flush() to ensure each command is sent and read / write stdout / stdin in separate threads or the I/O will stall.

  •  Tags:  
  • java
  • Related