【问题标题】:How to Execute Windows Commands Using Java - Change Network Settings如何使用 Java 执行 Windows 命令 - 更改网络设置
【发布时间】:2023-03-11 09:11:01
【问题描述】:

在 Java 中,我希望能够执行 Windows 命令。

有问题的命令是netsh。这将使我能够设置/重置我的 IP 地址。

请注意,我不想执行批处理文件。

我想直接执行这样的命令,而不是使用批处理文件。这可能吗?


这是我实施的未来参考解决方案:

public class JavaRunCommand {
    private static final String CMD = 
        "netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0";
    public static void main(String args[]) {

        try {
            // Run "netsh" Windows command
            Process process = Runtime.getRuntime().exec(CMD);

            // Get input streams
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            // Read command standard output
            String s;
            System.out.println("Standard output: ");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // Read command errors
            System.out.println("Standard error: ");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

【问题讨论】:

  • 这个问题已经回答了很多次了。只需查看 stackoverflow 建议即可找到其中一些建议
  • @SJuan76,我很抱歉。您能否将我与其中的一些问题联系起来?
  • @mre 看看侧边栏就行了。
  • @Matt Ball,facepalm...谢谢。

标签: java windows runtime.exec


【解决方案1】:
Runtime.getRuntime().exec("netsh");

参见RuntimeJavadoc。

编辑:leet 后来的回答表明这个过程现在已被弃用。但是,根据 DJViking 的评论,情况似乎并非如此:Java 8 documentation。该方法未被弃用。

【讨论】:

  • 在我的环境中,我必须在命令前加上“cmd /c”,即Runtime.getRuntime().exec("cmd /c netsh");
【解决方案2】:

使用ProcessBuilder

ProcessBuilder pb=new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process process=pb.start();
BufferedReader inStreamReader = new BufferedReader(
    new InputStreamReader(process.getInputStream())); 

while(inStreamReader.readLine() != null){
    //do something with commandline output.
}

【讨论】:

  • 这个结论的依据是什么?查看 javadoc,它没有被声明为已弃用。 docs.oracle.com/javase/8/docs/api/java/lang/…
  • 更新此问题:由于此问题在标记重复项时用作参考(即,我们将重复项指向此页面),因此最好澄清此答案,该答案比已接受的投票数更多回答。是否有此信息的来源?如果不是,也许应该将其作为错误答案删除?
  • Runtime.getRuntime().exec() 未被弃用。
【解决方案3】:

您可以使用Runtime.getRuntime().exec("<command>") 运行命令(例如Runtime.getRuntime().exec("tree"))。但是,这只会运行在路径中找到的可执行文件,而不是像echodel、...这样的命令,但只能运行像tree.comnetstat.com、...这样的命令要运行常规命令,你必须把命令前的cmd /c(如Runtime.getRuntime().exec("cmd /c echo echo")

【讨论】:

    【解决方案4】:
    public static void main(String[] args) {
        String command="netstat";
        try {
            Process process = Runtime.getRuntime().exec(command);
            System.out.println("the output stream is "+process.getOutputStream());
            BufferedReader reader=new BufferedReader( new InputStreamReader(process.getInputStream()));
            String s; 
            while ((s = reader.readLine()) != null){
                System.out.println("The inout stream is " + s);
            }                   
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    这行得通。

    【讨论】:

    • 每隔一行读取输出。应该是:String s; while ((s = reader.readLine() != null) { System.out.println(s); }
    【解决方案5】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2013-09-08
      相关资源
      最近更新 更多