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.
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.
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.
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