Solved

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

Posted on 1997-10-03
5
1,237 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
[X]
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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

724 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