Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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


Expert Comment

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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


Expert Comment

ID: 1223261
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".


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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

580 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