Solved

How do I hide the MSDOS window in a Java exec( <DOS cmd> )?

Posted on 1997-10-03
5
1,221 Views
Last Modified: 2013-11-18
I'm using Java's Runtime.exec() to set the parameters of a serial port (WinNT40). This works fine, except that the mode.com file needs a command interpreter to run. This too, is o.k., but in the process of running it flashes an MSDOS window on the screen. Does anyone know of a way to hide this  annoying feature?

I'd like this to run without the MSDOS window popping up and disappearing. BTW, I tried removing the "cmd.exe /c" part of the exec command, but that results in the mode command hanging my application and keeping the port busy until I kill it with NT's task manager. I've also tried adding the /q switch to NT40's cmd.exe but the pop-up window persisted.  Maybe this is an MSDOS question...

Anyway, here is the code (JVM 1.1). If anyone can find me a work-around I'd appreciate it!  
 
 

import java.io.*;

public class SerialTest
{
        public static void main( String args[] )
        {
                Runtime rt = Runtime.getRuntime();
                Process p = null;
                String portname = "com1:";
                String cmd[] = {"c:\\winnt\\system32\\cmd.exe", "/c",
                                               
"c:\\winnt\\system32\\mode.com", portname,
                                                "baud=9600", "parity=n",
"data=8",
                                                "stop=1", "to=on",
"xon=on", "octs=off", "odsr=off",
                                                "dtr=hs", "rts=hs" };
                try
                {
                        p = rt.exec( cmd );
                        if( p.waitFor() != 0 )
                        {
                                System.out.println("Error executing
command: " + cmd );
                                System.exit( -1 );
                        }
                        byte data[] = "This is a test of Java writing a
byte stream out of a serial port.".getBytes();
                        FileOutputStream fos = new FileOutputStream(
portname );
                        BufferedOutputStream bos = new
BufferedOutputStream( fos );
                        fos.write( data, 0, data.length );
                        fos.close();
                }
                catch( Exception e )
                {
                        System.out.println("exception: " +
e.getMessage());
                        e.printStackTrace();
                }
        }
}

0
Comment
Question by:roy020697
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:eugenem
ID: 1227896
Try this:
start /min cmd /c mode.com ...
0
 
LVL 1

Author Comment

by:roy020697
ID: 1227897
Eugene,

It sounded promising, but flamed out when implemented. The start command works fine from the command line, but does not work via Java Runtime.exec( "start", "/min", "cmd.exe" ...). I tried to find the "start" command on my hard drive, but it does not exist as an executable file. I guess it is an internal command.

The windows help example for start says that it will start an application from the command prompt. However, it is the command line interpreter "cmd.exe" which is causing my problem in the first place :-(

Thanks for the suggestion. Too bad it doesn't solve my problem.
0
 
LVL 3

Accepted Solution

by:
eugenem earned 200 total points
ID: 1227898
May be this will work?
cmd /c start /min mode.com
0
 
LVL 1

Author Comment

by:roy020697
ID: 1227899
That did the trick! I'd give you a better grade if:
1. you tried your solutions yourself first
2. you had the right answer the first time

If you did (1.) first, (2.) would be unnecessary.
Still, my problem is solved. I thank you very much!
0
 

Expert Comment

by:xjeffk
ID: 2781102
nice!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 43
servlet web applications   metadata-complete="true" or false 3 121
Eclipse for Java EE development 2 28
jsp error 6 26
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.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

828 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