How do I execute Windows commands in Java?

前端 未结 5 1685
遇见更好的自我
遇见更好的自我 2020-11-29 10:09

I\'m working on a project, and it will give you a list of Windows commands. When you select one, it will perform that command. However, I don\'t know how to do that. I was g

5条回答
  •  悲哀的现实
    2020-11-29 10:33

    an example. 1. create cmd 2. write to cmd -> call a command.

    try {
        // Execute command
        String command = "cmd /c start cmd.exe";
        Process child = Runtime.getRuntime().exec(command);
    
        // Get output stream to write from it
        OutputStream out = child.getOutputStream();
    
        out.write("cd C:/ /r/n".getBytes());
        out.flush();
        out.write("dir /r/n".getBytes());
        out.close();
    } catch (IOException e) {
    }
    

提交回复
热议问题