File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to run a batch file from a program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to run a batch file from a program?" Watch "How to run a batch file from a program?" New topic

How to run a batch file from a program?

Ravi Sathish
Ranch Hand

Joined: Feb 26, 2002
Posts: 131
Hi Ranchers
I hope this is the right forum for my question.
Let me state the problem I have at hand....
I have a batch file abc.bat with following lines:
cd c:\GoToDestination
java -cp runNewProgram
How to execute a batch file from my program?
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
You can use the Runtime class to execute the batch file. Use getRuntime() to access the current runtime environment as you cant create your own instance of the runtime class. The following is the code:

This returns a Process object.
Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101
You should do, on Windows NT4/2000/XP/2003:

Then on Windows 95/98/98SE/ME:

If the batch file does output at the console, then you must grab it.
Here is a more thorough explanation from JavaWorld.
I agree. Here's the link:
subject: How to run a batch file from a program?