Hi all,

I was looking for a way to execute windows batch file from java code, and I was able to do this with the following code.

Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("cmd /C " + "path" +exec.bat");

The batch file refers to some other files and executables in current directory and using relative path.

Since the java program is executed from a path different from the location of the batch file, the batch program cannot find the files/executables it is referring to

This leads to the batch program creating errors like

ERROR>>Could Not Find D:\xxx\Build App\BuildApp\xxxx.img

etc.....

Pls note :

D:\xxx\Build App\BuildApp\

is the path from where the java program is executed.

Can any one propose a solution for this.

Read this completely and thoroughly, and implement its suggestions.

Thank you for that link.
In fact it was this link that helped me get to the position where i am with the current program.
Perhaps i did not give enough details.
Here is the full story.

I am building a swing app, one which will execute few batch files, get the output from these batch files in real time and display them in a jtextarea.

I have accomplished this by using a streamgobbler to get the output from batch file and write it to the text area.

I use a swingworker thread to call a variation of "Listing 4.5 GoodWindowsExec.java" given in the link that you provided.

this works well with one of the batch files which does not do any operation based on current working directory.
Now the second batch file is something like this

del file1.a
del file2.b

..\..\util\test.exe abc.zip

etc....

this batch file works on the assumption that the files file1.a, file2.b and ..\..\util\test.exe exist in the current directory.

Now when i execute the java program, the current directory will be the one where the java program is residing.

this directory may not contain the files "file1.a, file2.b or the ..\..\util\test.exe directory

So the batch file does not produce expected output. it produces a whole lot of error saying file not found....

I can solve the problem by modifying the batch file and passing the path to the batch file. I will do this only as a last resort as these batch files are used by many people.

I hope i have made my problem clear.
Any suggestions to do this in java program itself would be very helpful..

Yes, the cwd is that of the java program, of course. The linked document (if I remember right) covers this point. If the batch script depends on cwd, then you are going to have to change the batch script or write another batch that does a cd then calls the batch script you actually wanted to call.

The other option is to read the API docs and pay attention to the ProcessBuilder class (and its directory method), as Runtime.getRuntime().exec() should not be done directly anymore. You have finer and fuller control using ProcessBuilder.

Thank you very much masijade for pointing me in the right direction.
Seems like the directory method of ProcessBuilder is exactly what i need.

though easier thing would be to

"write another batch that does a cd then calls the batch script you actually wanted to call"

This question has already been answered. Start a new discussion instead.