0

I have the following Java code to run a native Windows .exe file using ProcessBuilder

public class HMetis {
    private String exec_name = null;    
    private String[] hmetis_args = {"hmetis.exe", "null", "2", "1", "10", "1", "1", "1", "0", "0"};

    private Path path;
    private File file;

    public HMetis(String hgraph_exec, String hgraph_file) {
        this.exec_name = hgraph_exec;       
        this.hmetis_args[1] = hgraph_file;
    }       

    public void runHMetis() throws IOException {    
        this.path = Paths.get("C:\\hMetis\\1.5.3-win32");
        this.file = new File(path+"\\"+this.exec_name+".exe");      

        ProcessBuilder pb = new ProcessBuilder(this.hmetis_args);
        pb.directory(this.file);

        try {       
            Process process = pb.start();                       
        } finally {
            // do nothing
        }
    }
}

after running this code I am getting the below error although from the message it seems the directory name is fully formed and OK !! Any suggestions please?

Cannot run program "hmetis.exe" (in directory "C:\hMetis\1.5.3-win32\hmetis.exe"):CreateProcess error=267, The directory name is invalid
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Joarder Kamal
  • 1,387
  • 1
  • 20
  • 28
  • It seems that you are appending the executable name to the directory path - did you try without that? – Andreas Fester Jul 23 '13 at 12:11
  • 3
    Possible duplicate of [Running Windows .exe file with multiple arguments using Java ProcessBuilder is not producing any output file as expected](http://stackoverflow.com/questions/17809295/running-windows-exe-file-with-multiple-arguments-using-java-processbuilder-is-n) Edit your earlier question, rather than start new ones about the same problem. You also seem to have ignored the 1sat point of my advice to you there. So why should this duplicate question be any different? – Andrew Thompson Jul 23 '13 at 12:11
  • @Andrew not duplicate these are two different cases with different observations. Previously asked question says that the probably the .exe file is running without producing any error message but the .exe file was not working as it is expected to be. – Joarder Kamal Jul 23 '13 at 12:20
  • I will not be going to make any argument with you as you completely misunderstood the intention and I don't care whether anyone say it is a rubbish code or not. – Joarder Kamal Jul 23 '13 at 12:31

2 Answers2

1

You are using the complete path to the executable file as the ProcessBuilder's working directory:

this.file = new File(path+"\\"+this.exec_name+".exe");      
ProcessBuilder pb = new ProcessBuilder(this.hmetis_args);
pb.directory(this.file);
                    ^
                    |
                    ++++++++ "C:\hMetis\1.5.3-win32\hmetis.exe"
                             should be "C:\hMetis\1.5.3-win32"

However, you want to set the working directory only, like

pb.directory(this.path.toFile());

In addition, it seems that ProcessBuilder.directory() does not set the "working directory" as one might expect - at least not to find the executable. A similar issue is described at ProcessBuilder can't find file?!. At least on Windows, executables in the current working directory are usually found first (Unix is a different thing).

An easy fix would be to add the absolute path name to the command array, like

String[] hmetis_args = {"C:\\hMetis\\1.5.3-win32\\hmetis.exe", "null", "2", "1", "10", "1", "1", "1", "0", "0"};

See also

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • pb.directory() is expecting a File object therefore setting pb.directory(this.path) is producing error – Joarder Kamal Jul 23 '13 at 12:17
  • 1
    You can convert the `Path` object to `File` - see my updated answer. Probably an easier solution would be to use a `File` object directly - a `File` object can also represent a directory – Andreas Fester Jul 23 '13 at 12:19
  • after coverting the path.toFile() I am getting the following error: Exception in thread "main" java.io.IOException: Cannot run program "hmetis.exe" (in directory "C:\hMetis\1.5.3-win32"): CreateProcess error=2, The system cannot find the file specified – Joarder Kamal Jul 23 '13 at 12:23
  • regarding the updated answer: I did as you said by only using this.path omitting using this.file – Joarder Kamal Jul 23 '13 at 12:27
  • Are you sure that the path is correct? When I unzip http://glaros.dtc.umn.edu/gkhome/fetch/sw/hmetis/hmetis-1.5.3-WIN32.zip, it shows **hmetis-1.5.3-WIN32** (as one directory name, not a separate `hmetis` directory) – Andreas Fester Jul 23 '13 at 12:27
  • I created the dir by myself and copy the executables from the original .zip file – Joarder Kamal Jul 23 '13 at 12:29
  • 1
    See my updated answer - obviously `ProcessBuilder.directory()` behaves somewhat different from what I would expect from the docs ... – Andreas Fester Jul 23 '13 at 12:49
  • I followed your suggestions and use the absolute dir location as you said. Now Although I am not getting any error messages in Eclipse console but from stdout I am receiving the following message: ERROR> Can't open input file: No such file or directory I followed http://stackoverflow.com/questions/14165517/processbuilder-capturing-stdout-and-stderr-of-started-processes-to-another-stre to capture the stdout messages Thanks again Andreas for pointing me some links. I am now going through http://www.coderanch.com/t/517098/java/java/Working-ProcessBuilder-working-directory – Joarder Kamal Jul 23 '13 at 13:13
  • The updated solution provided by Gladiator at http://stackoverflow.com/questions/17809295/running-windows-exe-file-with-multiple-arguments-using-java-processbuilder-is-n?lq=1 worked for me. Many thanks once again for providing appropriate and correct suggestions. I will explore through this particular issue. Thanks :) – Joarder Kamal Jul 23 '13 at 13:25
0

Did you try to replace
pb.directory(this.file);
with
pb.directory(this.file.getParentFile()); ?

johan d
  • 2,798
  • 18
  • 26
  • pb.directory() is expecting a File object therefore setting pb.directory(this.path) is producing error – Joarder Kamal Jul 23 '13 at 12:18
  • doing as you suggested producing the below error: The method directory(File) in the type ProcessBuilder is not applicable for the arguments (String) – Joarder Kamal Jul 23 '13 at 12:37