Link to home
Start Free TrialLog in
Avatar of mbunkows
mbunkows

asked on

exec() a DOS program


Im trying to use the exec() from the java.lang.Runtime package to execute a DOS program.
I havent had any problems with starting up a Windows95 executable (ie: notepad) but when
I try to execute an executable that starts a DOS program or a batch file there are no exceptions but
the interpreter just hangs (more like ignores because I can do other things in the program).
Im just using it like:

Runtime r= Runtime.getRuntime();
r.exec("C:\\dos\\prog.exe"); // I also would like to be able to run a batch file from here

Is this possible?  



Avatar of gadio
gadio

mbunkows, try:
 r.exec("command /c c:/dos/prog.exe")
and same for batches.

Avatar of mbunkows

ASKER


It throws an IOException when I try that.
It DOES work from the command line, although I noticed that it doesnt open up a new DOS window it uses the one that is currently running (where the command was executed)
could this be where the problem is?
if the java interpreter is using command.com and then tries to create a process in the same window wouldnt that result in an exception like im getting?

I can either get rid of the java interpreter and then run the DOS program OR start the DOS program in a different window and then exit the java interpreter.  It really makes no difference.
Is this the root of my problem?
I dont know I only bang my head against the wall here (daily)

Thanks
did you try giving absolute path of command.com
Try this

c:\\winnt\system32\command.com /c call test.bat

Now, coming to problem, exec invokes win32 method CreateProcess with the first argument as NULL and second
argument as the command line you passed. Going thru the documentation of exec, we find that CreateProcess is invoked with creation flag as DETACHED_PROCESS (win32 funda). So, This means

"For console processes, the new process does not have access to the console of the parent process. The new process can call the AllocConsole function at a later time to create a new console. This flag cannot be used with the CREATE_NEW_CONSOLE flag."

To solve your problem, first write a C program which calls AllocConsole and creates a console and then uses the c standard function system() to invoke the batch file.

create the exe for the c program and invoke this exe in your Runtime.exec


If I remember well, exec() spawns an external process and returns directly. Because of
that, you have the impression that nothing is done. If you want the process to be finished
before continuing you have to write something like:

Runtime runtime = Runtime.getRuntime();
String  command = ...;
Process process = runtime.exec(command);
process.waitFor(); // the next line is executed AFTER the process has finished execution.
int status = process.exitValue(); // so that you know the status of the execution.




The process seems to be running in the background and hidden...
I noticed that when I shut down the system command.com was still running but was not visible at all...
also it doesnt seem to be doing anything even if i wait for the process to be finished executing before continuing like fontaine said.
the code was as follows:

Runtime r=Runtime.getRuntime();
Process process= r.exec("c:\\windows\\command.com /c dir > blah.txt");
process.waitFor();
int status= process.exitValue();
System.out.println("Status is: " + status);

There was no file called blah.txt created on my system and the status code came back
as "0"

Do I really need to open a different console with C and run a system call with that like evijay talked about?
It makes sense I guess...Java wasnt really written for the kind of things im trying to do with it
Curse platform dependence!

To give you some background on what Im trying to accomplish... Im trying to write a Java Menu system for a DOS program (as silly as that sounds) So what will eventually happen is the java program will call the DOS executable (with the proper arguments according to what button is clicked) and vice-versa

From the Java Programmer's FAQ (http://www.afu.com/javafaq.html):

10.7 So why can't I exec common DOS commands this way (as in 10.6)?
A. 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 builtin
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")

The following program runs for me (I am on an NT server):

public class Test {

    public static void main(String args[]) throws Exception {
        Runtime r=Runtime.getRuntime();
        Process process= r.exec("command.com /c dir > blah.txt");
        process.waitFor();
        int status= process.exitValue();
        System.out.println("Status is: " + status);
    }
}

Be also sure that the directory in which you are doing the test is not "read-only".

sorry it took me so long to respond

that seems to work I used the Java FAQ (i SWEAR ive read that thing 10 times and still keep finding new things.. with your help of course)

it even works when i use a short batch file
although when i call a longer batch file (13 lines seems to be the cutoff) the interpreter hangs (i cant even close the window normally)
the commands that are included in the batch file consist of setting environment variables etc (nothing that would take any amount of time)
also if i put 11 lines and 2 spaces it STILL doesnt work.. although if i remove the spaces it works
nothing in the batch file needs to be displayed in another DOS window (yet) and nothing requires user input
obviously im about pulling my hair out here

does this make any sense?  (it doesnt to me)
does calling a (long) batch file cause any problems for anyone else?


 
ASKER CERTIFIED SOLUTION
Avatar of evijay
evijay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sounds like a great solution however I havent been able to find the AllocConsole function in my compiler.
I do have one of the compilers you specified -- Borland C++ 3.0.
 
What library is AllocConsole in with your system?

I guess it makes some sense that a separate thread is needed for long batch files.

Hi,

I have VC++ and AllocConsole is in
Kernel32.lib. You need to include header file wincon.h

I think kernel32.lib must be present in Borland c++ since it is the
basic library in win32 programming. Best of luck

Vijay

I havent found the library in either Borland C++ or Builder but at least i know what to look for now and what I need to do.
Starting a new thread to run batches works like a charm
Thanks alot

under Win95 try this:

Runtime.getRuntime().exec("start /w command /c DOSprogram.exe");

that will run the DOS program in a new console