Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


exec() a DOS program

Posted on 1998-06-15
Medium Priority
Last Modified: 2013-11-18

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?  

Question by:mbunkows
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2

Expert Comment

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


Author Comment

ID: 1223257

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


Expert Comment

ID: 1223258
did you try giving absolute path of
Try this

c:\\winnt\system32\ /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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!


Expert Comment

ID: 1223259
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.


Author Comment

ID: 1223260
The process seems to be running in the background and hidden...
I noticed that when I shut down the system 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\\ /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


Expert Comment

ID: 1223261
From the Java Programmer's FAQ (

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 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(" /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(" /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".


Author Comment

ID: 1223262
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?


Accepted Solution

evijay earned 400 total points
ID: 1223263
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 !!)




Author Comment

ID: 1223264
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.


Expert Comment

ID: 1223265

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



Author Comment

ID: 1223266
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


Expert Comment

ID: 1223267
under Win95 try this:

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

that will run the DOS program in a new console

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This video teaches viewers about errors in exception handling.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question