We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,386 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Executing a batch file from java code.

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.

3
Contributors
5
Replies
1 Year
Discussion Span
8 Months Ago
Last Updated
9
Views
Question
Answered
sonu611
Newbie Poster
5 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Read this completely and thoroughly, and implement its suggestions.

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 17

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..

sonu611
Newbie Poster
5 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 17

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"

sonu611
Newbie Poster
5 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by masijade

Thanks

shahbaz07dbit
Newbie Poster
1 post since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0702 seconds using 2.7MB