This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run a batch file from a program?

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
TIA
Ravi
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic