Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中执行Windows命令?_Java_Windows_Cmd - Fatal编程技术网

如何在Java中执行Windows命令?

如何在Java中执行Windows命令?,java,windows,cmd,Java,Windows,Cmd,我正在做一个项目,它会给你一个Windows命令列表。当您选择一个时,它将执行该命令。然而,我不知道怎么做。我打算用Visual C或C++来做,但是C++类太复杂了,我不想在Visual C中创建表单和垃圾(在控制台应用程序中非常糟糕)。 您可以使用: Runtime.getRuntime().exec("ENTER COMMAND HERE"); 举个例子。 1.创建命令 2.写入cmd->调用命令 try { // Execute command String comma

我正在做一个项目,它会给你一个Windows命令列表。当您选择一个时,它将执行该命令。然而,我不知道怎么做。我打算用Visual C或C++来做,但是C++类太复杂了,我不想在Visual C中创建表单和垃圾(在控制台应用程序中非常糟糕)。 您可以使用:

Runtime.getRuntime().exec("ENTER COMMAND HERE");
举个例子。 1.创建命令 2.写入cmd->调用命令

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) {
}

利用
ProcessBuilder

它使构建流程参数变得更容易,并解决了自动在命令中使用空格的问题

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();
            }

        }

    }
}

我通常会构建一个通用类,您可以在“command”(比如“dir”)及其参数中传递该类,它会自动将调用附加到操作系统。如果命令允许输入,我还可以通过侦听器回调接口甚至输入来获取输出…

旧问题,但可能会帮助路过的人。这是一个简单有效的解决方案。上述一些解决方案不起作用

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();
        }
    }
}

来源:

这是在控制台窗口中运行和打印ipconfig命令输出的示例代码

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();
        }
    }
}
来源:

搜索“java运行命令”以帮助更好地细化问题-例如,哪些部分存在问题?请注意,有些命令在shell之外没有意义。其中包括
cd
等,应进行相应的仿真。(尽管我可能认为,在java中,模拟所有支持的命令——即移动/复制/列表/删除)是一种“更好的”投资,也可以打开一个真正的shell,让用户做任何他们想做的事情。如果你在Windows上,只使用VSExpress(Free)+C(实际上是和java一样的“困难”)。它只是工作(TM),包括WinForms。