changing the working-directory of command from java

33,442

Solution 1

To implement this you can use the ProcessBuilder class, here's how it would look like:

File pathToExecutable = new File( "resources/external.exe" );
ProcessBuilder builder = new ProcessBuilder( pathToExecutable.getAbsolutePath(), "-i", "input", "-o", "output");
builder.directory( new File( "resources" ).getAbsoluteFile() ); // this is where you set the root folder for the executable to run with
builder.redirectErrorStream(true);
Process process =  builder.start();

Scanner s = new Scanner(process.getInputStream());
StringBuilder text = new StringBuilder();
while (s.hasNextLine()) {
  text.append(s.nextLine());
  text.append("\n");
}
s.close();

int result = process.waitFor();

System.out.printf( "Process exited with result %d and output %s%n", result, text );

It's quite a bunch of code, but gives you even more control on how the process is going to be run.

Solution 2

Use this form of the exec method to specify a working directory

public Process exec(String[] cmdarray,
                    String[] envp,
                    File dir)
             throws IOException

The working directory is the third argument. You can pass null for envp if you don't need to set any special environment.

There's also this convenience method:

public Process exec(String command,
                    String[] envp,
                    File dir)
             throws IOException

...where you specify the command in one string (it just gets turned into an array for you; see the docs for details).

Solution 3

I goted a same problem in my project, I tryed this solutions about ProcessBuilder.directory(myDir) and the exec method from Runtime and all my trays failed..
It's make me understanding that the Runtime have a limited privileges just for the working directory and the sub directories of it.

So my solution is ugly but working very well.
I create a temporary .bat file in "runtime" in the working directory.
This file included two lines of commands:
1. Move to needed directory (cd command).
2. Do the command in need.
I call the exec from Runtime with my temporarily .bat file as command.
It's work for me very well!

Share:
33,442

Related videos on Youtube

user804723
Author by

user804723

Updated on June 20, 2020

Comments

  • user804723
    user804723 about 4 years

    I need to execute a .exe file from a function in one of the packages I have in my java project. now the working directory is the root directory of the project for java but the .exe file in sub-directories of my project. here is how the project is organized:

    ROOT_DIR
    |.......->com
    |         |......->somepackage
    |                 |.........->callerClass.java
    |
    |.......->resource
             |........->external.exe
    

    Initially I tried to run the .exe file directly through:

    String command = "resources\\external.exe  -i input -o putpot";
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(command);
    

    but the problem is external .exe needs to access some files in it's own directory and keeps thinking root directory is its directory. I even tried to use .bat file to solve the problem but the same issue rises:

    Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "resources\\helper.bat"});
    

    and the .bat file is in the same directory as the .exe file but the same issue happens. here is the content of the .bat file:

    @echo off
    echo starting process...
    
    external.exe -i input -o output
    
    pause
    

    even if I move .bat file to root and fix its content the problem does not go away. plz plz plz help

  • prusswan
    prusswan almost 13 years
    it looks reasonable but I cannot believe there is no simpler method to achieve the intended result
  • Matthew Ludwig
    Matthew Ludwig almost 3 years
    I know it's an old question/answer, but I really think the second most voted answer should be accepted over this one (the one saying to use the form of exec which takes 3 arguments). As the commenter above said, this is reasonable code but it's more complicated than necessary if all you want is to change the working directory.