Error: Caused by: java.io.IOException: CreateProcess error = 2, the system cannot find the file specified. Applies to all executable files

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

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

Now going back to code to accomplish the same thing, I am using processbuilder to do this. However, I end up getting the above error - java.io.IOException: The program "svn -version" cannot be run: CreateProcess error = 2, the system cannot find the file specified.

I have already tried the following things,

  • Using ProcessBuilder.environment checks the Path and PATH values. The path is required, but the PATH has all the required paths for applications, including the "TortoiseSVN / bin" path. So the theory cleanup ProcessBuilder has no executable location in its path.

  • At runtime, instead of just svn -version, I tried to give the full path, ie "C: / Program Files / TortoiseSVN / bin / svn.exe". This gave the same error too.

  • I tried the same code for another executable like "java -version", which also failed with the same exception.

I now have a feeling that something very basic is not right. But I've been trying to ponder this for over a day now, but I don't have any clues.

Ok one more thing, I am running this on a windows 7 box.

Below is the code 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.

Thanks, Vicky

+3


source to share


2 answers


This is because you are not using it ProcessBuilder

correctly. The Javadocs are pretty simple.

You cannot pass the argument --version

as part of the process name you are trying to call; it is not the file name of the process. Behind the scenes, you execute the process directly - there is no shell involved.



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

      

+8


source


You need to separate the executable from its arguments:

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

      



For more examples and examples, see the ProcessBuilder JavaDoc constructor .

+2


source







All Articles