File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to Run a batch file within Java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Run a batch file within Java code" Watch "How to Run a batch file within Java code" New topic
Author

How to Run a batch file within Java code

Mukunda s Dhakate
Greenhorn

Joined: Oct 29, 2007
Posts: 12
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

Joined: Aug 16, 2005
Posts: 14117
    
  16

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ladislav Honsa
Greenhorn

Joined: Jun 15, 2008
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Oct 29, 2007
Posts: 12
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Run a batch file within Java code