How to call a DOS- or Windows-Program from a Java-Program

The code looks like:

...
...
if(true)
  call_dos_windows_program()  // such as cmd or wordpad
...
...
chenweiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
GrandSchtroumpfConnect With a Mentor Commented:
   String wincommand  = "Notepad";
    Runtime rt = Runtime.getRuntime();
    try {
      Process p = rt.exec(wincommand);
    } catch (Exception wine) {
      wine.printStackTrace();
    }
0
 
chenweiAuthor Commented:
To GrandSchtroumpf:

It runs if I start "notepad". But if I want to start "wordpad", I got exeptions. And if I want to start a BAT-file, I can see that , though it's started.

Why?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
Mayank SAssociate Director - Product EngineeringCommented:
Try running the batch-file like this: cmd.exe /C start cmd.exe BatchFileName
0
 
objectsCommented:
> But if I want to start "wordpad", I got exeptions

what exceptions?
0
 
GrandSchtroumpfCommented:
the wincommand in my code will work if it works in a command prompt.
if you open a command prompt and type "nodepad", notepad opens.
if you open a command prompt and type "wordpad", you get an error.
it's up to you to find the correct windows command.
if you want to open wordpad for example, you might need to use the complete path for wordpad (i.e. "C:\Program Files\Windows NT\Accessories\wordpad.exe").
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I guess it would still not open if it does not open from the command prompt. Some programs can only be invoked from inside Windows, and not from the console. I tried Wordpad - gave me exceptions too.
0
 
GrandSchtroumpfCommented:
:°)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.