File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes running bat file from java app. PLEASE HELP!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "running bat file from java app. PLEASE HELP!!!!!" Watch "running bat file from java app. PLEASE HELP!!!!!" New topic
Author

running bat file from java app. PLEASE HELP!!!!!

Priya Jakkinpali
Greenhorn

Joined: Mar 17, 2006
Posts: 3
Hi All,
I am trying to run a bat file from my java app using the following command:

Runtime r = Runtime.getRuntime();
r.exec("C:\\BatchJobs\\test.bat");

My test.bat file contains the following:

cd C:\BatchBobs
cp test1.txt test2.txt


However, when i run my app, the test2.txt file is not created. When i run the test.bat file from command line, it works perfectly fine.

Also, another thing, if I try to run an exe file using the same java code as above, it works.
e.g: i thried this:

r.exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE");

When i ran my java program, it successfully opened Microsoft Excel.

Why cant I run my bat file?
Please HELP!!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Because a batch file isn't an executable program, and Runtime.exec() is for running executable programs. The way to do it is to run the Windows command shell (you probably know that as the "Command Prompt" program) and ask it to run the batch file, like this:
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Take a look at:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Guy
Priya Jakkinpali
Greenhorn

Joined: Mar 17, 2006
Posts: 3
Hi Paul Clapham,
I tried your method. Still does not work. Same problem.
-Priya
Priya Jakkinpali
Greenhorn

Joined: Mar 17, 2006
Posts: 3
Never mind. I got this figured out.
I changed my code to:

Process p = r.exec("cmd /c start C:/BatchJobs/test.bat");

This works just fine.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: running bat file from java app. PLEASE HELP!!!!!