Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Priya Jakkinpali
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 776
 
Priya Jakkinpali
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul Clapham,
I tried your method. Still does not work. Same problem.
-Priya
 
Priya Jakkinpali
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic