【问题标题】:trying to execute a batch file in java试图在java中执行批处理文件
【发布时间】:2014-07-10 04:37:49
【问题描述】:

为什么该代码不起作用?

Process p1 = Runtime.getRuntime().exec("switchCon.bat");

这是该文件的路径:

C:\Users\Administrator\Desktop\switchCon.bat

这是文件的样子:

netsh interface ip set address "Local Area Connection 2" static 147.8.178.231 255.255.255.0 147.8.176.1 1

【问题讨论】:

  • 不起作用是什么意思?
  • 您的应用程序在哪个目录上下文中运行(相对于批处理文件)?有什么错误吗?
  • 使用完整路径和/或使用cmd /c c:\\path\\to\\file.bat

标签: java windows bash batch-file native-code


【解决方案1】:

这个怎么样:

Process pr;
Runtime run = Runtime.getRuntime();
pr = run.exec("cmd.exe /c C:\\Users\\Administrator\\Desktop\\switchCon.bat"); 
pr.waitFor();

或者这个:

Process pr;
Runtime run = Runtime.getRuntime();
pr = run.exec("cmd.exe /c netsh interface ip set address "Local Area Connection 2" static 147.8.178.231 255.255.255.0 147.8.176.1 1"); 
pr.waitFor();

【讨论】:

    【解决方案2】:

    尝试指定bat文件的完整路径,如下:

    Process p1 = Runtime.getRuntime().exec("C:\\Users\\Administrator\Desktop\\switchCon.bat");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 2017-02-15
      • 2021-11-24
      相关资源
      最近更新 更多