How do I execute Windows commands in Java?

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

    Old question but might help someone passing by. This is a simple and working solution. Some of the above solutions don't work.

    import java.io.IOException;
    import java.io.InputStream;
    
    public class ExecuteDOSCommand
    {
        public static void main(String[] args)
        {
            final String dosCommand = "cmd /c dir /s";
            final String location = "C:\\WINDOWS\\system32";
            try
            {
                final Process process = Runtime.getRuntime().exec(dosCommand + " " + location);
                final InputStream in = process.getInputStream();
                int ch;
                while((ch = in.read()) != -1)
                {
                    System.out.print((char)ch);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    

    Source: http://www.devx.com/tips/Tip/42644

    0 讨论(0)
  • 2020-11-29 10:28

    Take advantage of the ProcessBuilder.

    It makes it easier to build the process parameters and takes care of issues with having spaces in commands automatically...

    public class TestProcessBuilder {
    
        public static void main(String[] args) {
    
            try {
                ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "dir");
                pb.redirectError();
                Process p = pb.start();
                InputStreamConsumer isc = new InputStreamConsumer(p.getInputStream());
                isc.start();
                int exitCode = p.waitFor();
    
                isc.join();
                System.out.println("Process terminated with " + exitCode);
            } catch (IOException | InterruptedException exp) {
                exp.printStackTrace();
            }
    
        }
    
        public static class InputStreamConsumer extends Thread {
    
            private InputStream is;
    
            public InputStreamConsumer(InputStream is) {
                this.is = is;
            }
    
            @Override
            public void run() {
    
                try {
                    int value = -1;
                    while ((value = is.read()) != -1) {
                        System.out.print((char)value);
                    }
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
    
            }
    
        }
    }
    

    I'd generally build a all purpose class, which you could pass in the "command" (such as "dir") and it's parameters, that would append the call out to the OS automatically. I would also included the ability to get the output, probably via a listener callback interface and even input, if the command allowed input...

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-29 10:31

    I hope this helps :)

    You could use:

    Runtime.getRuntime().exec("ENTER COMMAND HERE");
    
    0 讨论(0)
  • 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) {
    }
    
    0 讨论(0)
提交回复
热议问题