permaculture playing cards*
The moose likes Java in General and the fly likes Launching a DOS batch file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Launching a DOS batch file" Watch "Launching a DOS batch file" New topic
Author

Launching a DOS batch file

Jean Paul Martin
Ranch Hand

Joined: Oct 05, 2003
Posts: 30
Hi,

I am trying to launch a DOS batch file from my JAVA program. The following code works for .exe only, eg. launching dialer.exe The batch file will contain DOS command to read an ldf file and insert to LDAP:





Do I even need the batch file ??? Or, is there a way to simply enter the DOS commands in Java like the dialer.exe example above? Thanks a lot, I really appreciate your help.

Jean Paul
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This question from the Java Programmer's FAQ is relevant. The online version is a mess right now so I'll copy this for you from my local copy:


# (Sect. 18) So why can't I exec common DOS commands this way (as in 18.7)?

The reason is that many of the DOS commands are not individual programs, but merely "functions" of command.com. There is no DIR.EXE or COPY.EXE for example. Instead, one executes the command processor (shell) explicitly with a request to perform the built-in command, like so:
Runtime.getRuntime().exec("command.com /c dir") for example. On NT, the command interpreter is "cmd.exe", so the statement would be Runtime.getRuntime().exec("cmd /c dir") And you could bring the command output into the program with code like:



This situation occurs on any OS where some commands are actually interpreted directly by the shell.




[Jess in Action][AskingGoodQuestions]
Jean Paul Martin
Ranch Hand

Joined: Oct 05, 2003
Posts: 30
Hi Mr. Friedman,

Thank you for your reply. This is what I am trying to do. I have a JMenu created and when i click on it i am trying to execute the following DOS arguments:

ldifde -i -f test.ldf servername

where test.ldf is my test file. So, I tried doing:


But, it doesn't work. Could somebody please let me know how to bring up a DOS shell that will display the above DOS arguments with its results and then close it.

Thank you for your help, I really appreciate.

Jean Paul
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
If you use .bat or .cmd it should work fine...

All of those work.
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
Originally posted by Jean Paul Martin:



Remove the colon?
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Darin Niard:
Remove the colon?


Instead of colon, put a space between /c and ldifde. Hope it helps...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Jean Paul Martin
Ranch Hand

Joined: Oct 05, 2003
Posts: 30
Hi Everyone,

Thank you all for your replies. However, i still do not get it to work. Here is what is happening in more detail:
I have my Menu Action here:


and my toolsBatchAdd() is:


But here's my problem: my Frame freezes when i execute the method. I am thinking it could be because I have a DOS window already open and I run do "java myprogramname". So, then since java is already being executed, my DOS command "ldifde -i -f test.ldf servername" hangs. I tested this command and it works great. So, I am not really sure how to proceed further!! I also tried launching a separate DOS window, but that didn't work either. Another note, my local drive is E: and the textDisplay.message above is just a textbox where in my frame.

Could you guys please help me out in Opening a Separate DOS window and execute my commands from there??? Thank you so much for your help and have a great day.

Jean Paul
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
If that command writes to stdout or stderr, you MUST consume those streams.
If you don't, the whole thing will hang.
See the Process documentation. This comes up alot so you might find more by searching here.
Bill
Jean Paul Martin
Ranch Hand

Joined: Oct 05, 2003
Posts: 30
How do I go about consuming the stream please ? Thank you.

Jean Paul
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
Be sure to read the JavaDocs for java.lang.Process - note the methods getOutputStream and getErrorStream.

There is an example in on of the first responses to your question.

For more examples of using those streams, a google search for "java exec getOutputStream" found this example, along with many others.
[ June 26, 2004: Message edited by: William Brogden ]
Jean Paul Martin
Ranch Hand

Joined: Oct 05, 2003
Posts: 30
Thank You Sir for your great help. I appreciate. Have a great day.

Jean Paul
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Launching a DOS batch file