Java CreateProcess error=2, 系统找不到指定的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19621838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:52:53  来源:igfitidea点击:

CreateProcess error=2, The system cannot find the file specified

javaprocess

提问by Trijit

I am writing a program in java which would execute winrar and unzip a jar file for me placed in h:\myjar.jarinto the folder h:\new. My java code goes something like this

我正在用 java 编写一个程序,它将执行 winrar 并为我解压缩一个 jar 文件放入h:\myjar.jar文件夹中h:\new。我的java代码是这样的

import java.io.File;
import java.io.IOException;

public class MainClass {

    public static void main(String[] args) {

        Runtime r = Runtime.getRuntime();
        Process p = null;

        try {
            File dir = new File("C:/Program Files/WinRAR");
            p = r.exec("winrar x h:\myjar.jar *.* h:\new", null, dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

When I execute this, I am getting this error

当我执行此操作时,出现此错误

java.io.IOException: Cannot run program "winrar" (in directory "C:\Program Files\WinRAR"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at MainClass.main(MainClass.java:16)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 4 more

Can anyone tell me why am I encountering such a problem. What is the change I need to incorporate in code so that it works?

谁能告诉我为什么我会遇到这样的问题。我需要在代码中进行哪些更改才能使其正常工作?

采纳答案by MadProgrammer

Assuming that winrar.exeis in the PATH, then Runtime.execis capable of finding it, if it is not, you will need to supply the fully qualified path to it, for example, assuming winrar.exeis installed in C:/Program Files/WinRARyou would need to use something like...

假设它winrar.exe在 中PATH,然后Runtime.exec能够找到它,如果没有,您将需要提供完全限定的路径,例如,假设winrar.exe安装在C:/Program Files/WinRAR您需要使用类似...

p=r.exec("C:/Program Files/WinRAR/winrar x h:\myjar.jar *.* h:\new");

Personally, I would recommend that you use ProcessBuilderas it has some additional configuration abilities amongst other things. Where possible, you should also separate your command and parameters into separate Stringelements, it deals with things like spaces much better then a single Stringvariable, for example...

就个人而言,我建议您使用ProcessBuilder它,因为它具有一些额外的配置功能。在可能的情况下,您还应该将命令和参数分成单独的String元素,它比单个String变量更好地处理诸如空格之类的事情,例如......

ProcessBuilder pb = new ProcessBuilder(
    "C:/Program Files/WinRAR/winrar",
    "x",
    "myjar.jar",
    "*.*",
    "new");
pb.directory(new File("H:/"));
pb. redirectErrorStream(true);

Process p = pb.start();

Don't forget to read the contents of the InputStreamfrom the process, as failing to do so may stall the process

不要忘记InputStream从进程中读取 的内容,因为不这样做可能会使进程停止

回答by Reimeus

The complete first argument of execis being interpreted as the executable. Use

完整的第一个参数exec被解释为可执行文件。用

p = rt.exec(new String[] {"winrar.exe", "x", "h:\myjar.jar", "*.*", "h:\new" }
            null, 
            dir);

回答by viator

The diryou specified is a working directory of running process - it doesn't help to find executable. Use cmd /c winrar ...to run process looking for executable in PATHor try to use absolutepath to winrar.

dir您指定的是正在运行的进程的工作目录-它并不能帮助找到可执行文件。使用cmd /c winrar ...运行过程中寻找可执行文件路径或尝试使用绝对路径WinRAR的

回答by EagleEye1984

I used ProcessBuilder but had the same issue. The issue was with using command as one String line (like I would type it in cmd) instead of String array. In example from above. If I ran

我使用了 ProcessBuilder 但遇到了同样的问题。问题在于使用命令作为一个字符串行(就像我在 cmd 中输入它一样)而不是字符串数组。在上面的例子中。如果我跑

ProcessBuilder pb = new ProcessBuilder("C:/Program Files/WinRAR/winrar x myjar.jar *.* new");
pb.directory(new File("H:/"));
pb. redirectErrorStream(true);

Process p = pb.start();

I got an error. But if I ran

我有一个错误。但是如果我跑了

ProcessBuilder pb = new ProcessBuilder("C:/Program Files/WinRAR/winrar", "x", "myjar.jar", "*.*", "new");
pb.directory(new File("H:/"));
pb. redirectErrorStream(true);

Process p = pb.start();

everything was OK.

一切正常。

回答by Grim

My recomendation is to keep the getRuntime().execbecause execuses the ProcessBuilder.

我的建议是保留getRuntime().exec因为exec使用ProcessBuilder.

Try

尝试

 p=r.exec(new String[] {"winrar", "x", "h:\myjar.jar", "*.*", "h:\new"}, null, dir);