我对 Java 中的 Runtime.exec() 有疑问 我的代码:
String lol = "/home/pc/example.txt";
String[] b = {"touch", lol};
try {
Runtime.getRuntime().exec(b);
} catch(Exception ex) {
doSomething(ex);
}
它运行良好,但当我尝试 changle 变量时,“lol”文件不会在硬盘中创建
例如:
String lol = x.getPath();
其中 getPath() 返回 String
我该怎么办?
感谢您的回复:)
最佳答案
这是您的问题的解决方案。我遇到了类似的问题,这通过指定输出目录对我有用,它应该在该工作目录中执行文件的输出。
ProcessBuilder proc = new ProcessBuilder("<YOUR_DIRECTORY_PATH>" + "abc.exe"); // <your executable path>
proc.redirectOutput(ProcessBuilder.Redirect.INHERIT); //
proc.directory(fi); // fi = your output directory
proc.start();
关于Java - Runtime.getRuntime().exec() 这是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3032398/