0

爪哇

public static void startCmd(String path)
{
  Runtime rt = Runtime.getRuntime();
  Process pr = rt.exec(path);
  BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "cp852")); 
  String line = null;

  // redirection output to console
  while((line = input.readLine()) != null) {
    System.out.println(line);
  }
  System.out.println("Error code: " + pr.waitFor());
}

public static void main(String[] args) {
  startCmd("c:\\run.bat");
}

运行.bat

cd c:\cmd\
application.exe

控制台输出

D:\WORKSPACE\TEST>cd c:\cmd\ 
D:\WORKSPACE\TEST>application.exe // path should be changed from D:\WORKSPACE\TEST> to c:\cmd>
Error code: 0

为什么cd c:\cmd\命令没有传递给 java 控制台应用程序???

4

2 回答 2

2

在 下DOS,当更改到另一个驱动器的路径时,必须先使用驱动器号,然后才能设置目录。这是因为每个驱动器都有自己的工作目录。您需要添加C:到批处理文件:

C:
cd c:\cmd\
application.exe
于 2013-05-21T13:37:09.693 回答
1
cd c:\cmd\
c:
application.exe

尝试这个。

于 2013-05-21T13:38:10.413 回答