【问题标题】:java.io.IOException: The system cannot find the file specified [duplicate]java.io.IOException:系统找不到指定的文件[重复]
【发布时间】: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");

标签: java eclipse


【解决方案1】:

我认为系统找不到文件的原因是directory 方法只设置了构建器运行的进程的工作目录(来自Javadocs):

设置此流程构建器的工作目录。随后由该对象的 start() 方法启动的子进程将使用它作为它们的工作目录。

所以你需要这个:

builder.command("..\\n.txt");

让系统找到你的文件。这仍然不会做任何有用的事情,您会收到类似于以下内容的错误:

Cannot run program "..\n.txt" (in directory ".."): CreateProcess error=193, %1 is not a valid Win32 application

流程构建器需要您的操作系统中的有效应用程序。

【讨论】:

  • builder 会在当前工作目录中查找文件,获取方法见stackoverflow.com/questions/4871051/…
  • 我第一次没有很好地解释自己:我实际上是想运行一个 exe。如果我将您的想法用于 .command("..\test.exe");它现在说:请求的操作需要提升
  • @murkr 由于某些安全问题,您尝试运行的进程无法运行,这是另一个问题!看起来可能与 Windows UAC see this question 有关
猜你喜欢
  • 1970-01-01
  • 2014-04-24
  • 2018-09-17
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多