Error: Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified. Applies for all executables

為{幸葍}努か 提交于 2019-11-28 08:51:24

问题


My goal is to run SVN commands from java for one of my requirements, for the same i have already installed TortoiseSVN command line tool. Added the appropriate path"C:/Program Files"/TortoiseSVN/bin" to my environment "Path" variable.

With the above setup, i can run my svn commands from windows command line using say "svn --version" and it works perfectly fine.

Now coming back to the code to execute the same, i am using processbuilder for this. However i end up with the above error - java.io.IOException: Cannot run program "svn --version": CreateProcess error=2, The system cannot find the file specified.

I tried following things already,

  1. Using ProcessBuilder.environment checked the Path and PATH values. Path is emply, but PATH has all the necessary application paths configured including "TortoiseSVN/bin" path. So that clears the theory of ProcessBuilder not have executable location in its path.

  2. While executing, instead of just svn --version i tried giving the complete path i.e. "C:/Program Files/TortoiseSVN/bin/svn.exe". That too gave the same error.

  3. I tried the same code for other executable like "java -version" that too failed with the same exception.

I now have a feeling something very basic is not right. But tried hitting my head around this for more than a day now, but i ain't getting any clues.

Ok one more thing, i am running this on Windows 7 box.

Below is the code that i am using,

    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class RunningExecutable {

public static void main(String[] args){
    String command = "svn --version";

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {           
        ProcessBuilder svnProcessBuilder = new ProcessBuilder(command);
        String PATH = svnProcessBuilder.environment().get("PATH");
        System.out.println("PATH - " + PATH);

        String path = svnProcessBuilder.environment().get("Path");
        System.out.println("Path - " + path);

        Process procObject = svnProcessBuilder.start();

        BufferedReader cmdStreamReader = new BufferedReader(new InputStreamReader(procObject.getInputStream()));
        String cmdOutput;
        while ((cmdOutput = cmdStreamReader.readLine()) != null) {
            outputStream.write((cmdOutput + "\n").getBytes());
        }
        System.out.println("O/p - " + outputStream.toString());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Throwable th) {
        th.printStackTrace();
    }
}
    }

Looking forward to any hints/pointers at all.

Thanks, Vicky


回答1:


It's because you're not using ProcessBuilder correctly. The Javadocs are pretty clear cut.

You can't pass the --version argument as part of the process name you're trying to invoke; that's not the filename of the process. Behind the scenes you're exec'ing a process directly - there's no shell involved.

ProcessBuilder svnProcessBuilder = new ProcessBuilder("svn", "--version");



回答2:


You need to separate the executable from its arguments:

new ProcessBuilder("svn", "--version")

See the ProcessBuilder constructor JavaDoc for more details and examples.



来源:https://stackoverflow.com/questions/9644016/error-caused-by-java-io-ioexception-createprocess-error-2-the-system-cannot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!