Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

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 + "
").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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...