• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to Run a batch file within Java code

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80765
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80765
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 5119
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic