• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

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?  

  • 5
  • 3
  • 2
  • +2
1 Solution
mbunkows, try:
 r.exec("command /c c:/dos/prog.exe")
and same for batches.

mbunkowsAuthor Commented:

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)

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

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

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.

mbunkowsAuthor Commented:
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");
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");
        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".

mbunkowsAuthor Commented:
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?

You need to run the longer batch files in a seperate thread. Not in the main thread. As i told you earlier remember one thing.

Which ever command is executed using Runtime.exec, that command will not have a console associated with it !!. This means, what ever output the command prints, it wont be displayed. If the command expects some input, it cant accept!!

So, The only solution is to just call AllocConsole in a c program which inturn may accept a batch file as command line argument and runs the batch file. a sample c program is here

main (int argc, char **argv)
   if (argc <= 1) {
       char dummyline[100];
       printf("usage : test.exe batchfilename\n");
       scanf("%s\n", dummyline);

Once, you compile this (u have to use VC++ or borland c++ and specify appropriate library for AllocConsole function), write your java program like this

Runtime.exec("text.exe mybatchfile.bat");

this will work (it worked in my system !!)



mbunkowsAuthor Commented:
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.


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


mbunkowsAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now