在我的 jar 应用程序中,我在 exe 程序中进行了一些计算。当文件和program.exe位于同一目录时,我使用了以下命令:
String[] str={"program.exe", "file1.txt", "file2.txt"};
pr = rt.exec(str);
效果很好。但是当我将文件移动到其他目录并尝试使用此命令时:
String[] str={"program.exe", "temp\\file1.txt", "temp\\file2.txt"};
pr = rt.exec(str);
program.exe 看不到文件。更奇怪的是,当我将其名称更改为其他默认值时,它开始看到文件。 file1.txt、file2.txt和temp是在program.exe启动之前在我的jar程序中创建的。
编辑:
当问题开始时,我尝试这样的操作:默认名称 file1.txt 和 file2.txt,我更改为 aaa.txt 和 bbb.txt (在 Windows 中),然后:
String[] str={"program.exe", "temp\\aaa.txt", "temp\\bbb.txt"};
并且它有效。
编辑2:
现在我知道问题出在program.exe 中。当我从命令行(而不是从 jar )使用它时,如下所示:
program.exe temp\file1.txt temp\file2.txt
错误:
FANN Error 1: Unable to open configuration file "temp\file1.txtÉ║@" for reading.
fann 是人工神经网络库。当我将文件复制到program.exe目录时:
program.exe file1.txt file2.txt
它有效!当我更改 temp 中的文件名并执行以下操作时:
program.exe temp\file1aaa.txt temp\file2bbb.txt
它也有效!那么这是 fann lib bug 吗?
最佳答案
我会使用 ProcessBuilder api(它为您提供比 Runtime.exec() 更多的控制权),并且我还会使用绝对路径:
File directory = new File("/path/tp/program.exe's/parent");
int returnCode = new ProcessBuilder("program.exe",
new File(directory, "temp/file1.txt").getAbsolutePath(),
new File(directory, "temp/file2.txt").getAbsolutePath()
)
.directory(directory).start().waitFor();
关于java - exec,文件位置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7362160/