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

The code looks like:

  call_dos_windows_program()  // such as cmd or wordpad
Who is Participating?

Improve company productivity with a Business Account.Sign Up

GrandSchtroumpfConnect With a Mentor Commented:
   String wincommand  = "Notepad";
    Runtime rt = Runtime.getRuntime();
    try {
      Process p = rt.exec(wincommand);
    } catch (Exception wine) {
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.

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
> But if I want to start "wordpad", I got exeptions

what exceptions?
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").
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.
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.