How do I execute Windows commands in Java?

前端 未结 5 1666
遇见更好的自我
遇见更好的自我 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:30

    This is a sample code to run and print the output of the ipconfig command in the console window.

    import java.io.IOException;
    import java.io.InputStream;
    
    public class ExecuteDOSCommand {
        public static void main(String[] args) {
            final String dosCommand = "ipconfig";
            try {
                final Process process = Runtime.getRuntime().exec(dosCommand );
                final InputStream in = process.getInputStream();
                int ch;
                while((ch = in.read()) != -1) {
                    System.out.print((char)ch);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Source: https://www.codepuran.com/java/execute-dos-command-java/

提交回复
热议问题