【发布时间】:2015-02-26 16:58:17
【问题描述】:
我的代码:
ProcessBuilder builder = new ProcessBuilder();
builder.command("n");
File file = new File("..");
builder.directory( file );
try {
Process p = builder.start();
} catch (Exception e) {
System.out.println(e);
}
Eclipse 说:
java.io.IOException
Cannot run program "n" (in directory ".."): CreateProcess error=2, The system cannot find the file specified
但是文件 n.txt 在那里,如果我说:
for(String fileNames : file.list()) System.out.println(fileNames);
已列出:n.txt。
如果我在源代码中将“n.txt”替换为“n”或尝试调用.exe,则会出现同样的问题;
System.out.println(file.getCanonicalPath());
创造
F:\Programme\eclipse\workspaces\test
这个:
System.out.println(builder.directory().getAbsolutePath());
创造
F:\Programme\eclipse\workspaces\SimulatorAddOn\SimulatorAddOn\..
然后替换
ProcessBuilder builder = new ProcessBuilder();
builder.command("n");
与
ProcessBuilder builder = new ProcessBuilder("n");
也不会改变任何东西。
我需要你们的帮助。 提前致谢
【问题讨论】:
-
为什么要运行文本文件?如果想用默认程序打开,见stackoverflow.com/questions/550329/…
-
就像我说的,我实际上尝试在同一个文件夹中运行一个 .exe。如果我说 Desktop.getDesktop().open(file); n.txt 打开,但我必须调整为 File file = new File("..\\n.txt");