• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Run a batch file within Java code

 
Mukunda s Dhakate
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to Run a Batch file from my Java code.
I m trying it with Runtime.exec("");
But still there is no success.
Please any one help.

I have tried Runtime.exec("cmd /C D:\\aaa.bat").

Thank you
Regards
Mukunda
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15458
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dhakate Mukunda:
But still there is no success.

What does that mean exactly? Do you get error messages? If so, then what are the error messages?
 
Ladislav Honsa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The exec() method is not static so you first need an instance of the Runtime object. Furthermore you can just directly call the batch file from the exec() method.
The following should work:

Process exec = Runtime.getRuntime().exec("D:\\aaa.bat");

Of course you have to surround it with a try/catch block.

Regards,
Ladislav
 
Campbell Ritchie
Sheriff
Pie
Posts: 49858
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ladislav Honsa, welcome to JavaRanch.

It is actually much more complicated than that to use Runtime.getRuntime().exec(). What happens is that you have to wait until the process completes, then it may send messages (Strings) via two streams, one representing standard output, the other representing standard error. Both these streams need to be kept "empty" otherwise you will get deadlock. The ProcessBuilder class introduced in Java5 makes the whole thing easier, by allowing you to "merge" the two streams, but to all intents and purposes you still need to use Daconta's method.
I had a similar problem this time last year, and sorted it out with my StreamEater class, which you can see here,; it is only slightly changed from Daconta.
 
Mukunda s Dhakate
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime rt = Runtime.getRuntime();
rt.exec("cmd /C start /min "+strBatchPath);

this is working for windows XP,
but not working on the Windows 2000.
please help
 
Campbell Ritchie
Sheriff
Pie
Posts: 49858
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read Daconta as I quoted yesterday, Dhakate Mukunda?
Look at the API documentation for Runtime#exec. It has a return value of a Process. How are you handling that Process object?
 
Norm Radder
Bartender
Posts: 1241
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not all versions of Windows have the command "cmd". Some versions would use 'command' to execute a batch file.

Try these to see if one works on your system.
[ June 18, 2008: Message edited by: Norm Radder ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic