This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 18541
    
    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
 
Don't get me started about those stupid light bulbs.
 
subject: running bat file from java app. PLEASE HELP!!!!!
 
Similar Threads
Why is Runtime.getRuntime().exec() not working in JSP?
Running a .bat file with Java code
run bat file within java
problem running .BAT file from the Java app
How to execute .BAT thru java Application