gpt4 book ai didi

Java 运行时.exec : doesn't change path

转载 作者:行者123 更新时间:2023-12-01 23:48:11 27 4
gpt4 key购买 nike

java:

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

run.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 控制台应用程序???

最佳答案

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

C:
cd c:\cmd\
application.exe

关于Java 运行时.exec : doesn't change path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16671235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com