Solved

exec() a DOS program

Posted on 1998-06-15
12
357 Views
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?  



0
Comment
Question by:mbunkows
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 6

Expert Comment

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

0
 

Author Comment

by:mbunkows
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)

Thanks
0
 
LVL 4

Expert Comment

by:evijay
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


0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Expert Comment

by:fontaine
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.




0
 

Author Comment

by:mbunkows
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");
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

0
 
LVL 5

Expert Comment

by:fontaine
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");
        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".

0
 

Author Comment

by:mbunkows
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?


 
0
 
LVL 4

Accepted Solution

by:
evijay earned 100 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
test.c


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


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

vijay


}

0
 

Author Comment

by:mbunkows
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.

0
 
LVL 4

Expert Comment

by:evijay
ID: 1223265
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

0
 

Author Comment

by:mbunkows
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

0
 

Expert Comment

by:mmorones
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 84
Java Options in Zimbra Server 2 54
tomcat not starting 6 45
Facing this issue for maven proxy setting 2 12
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.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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 …

809 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