user14864
user14864

Reputation: 341

Execute a windows command using java's Runtime package

this command works perfectly if I do it in the command prompt:

start Outlook /a C:\Users\Steve\Desktop\test.jpg

However when I try to execute it in java using the getRuntime().exec() method it gives me an error " Cannot run program "start": CreateProcess error=2, The system cannot find the file specified "

here is the code I am using:

 String command = "start Outlook /a C:\\Users\\Steve\\Desktop\\test.jpg";
    try {
        Runtime.getRuntime().exec(command);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

not sure what I am doing wrong here. The start command is a valid command in the command prompt, I don't know how to get it to work with java's .exec() method

Upvotes: 0

Views: 188

Answers (3)

tbodt
tbodt

Reputation: 17017

The command prompt is a bit different from what is done with Runtime.exec. start is a command that is provided by the prompt, but is not a real program. To make this work, you can invoke the prompt to run Outlook:

String command = "cmd /c start Outlook /a C:\\Users\\Steve\\Desktop\\test.jpg";

Upvotes: 1

Jason C
Jason C

Reputation: 40426

start is not a program; there is no start.exe, it is just a shell command.

It's purpose is to start a program in a new command window. You'd have to use cmd to run that command:

String command = "cmd /c start Outlook /a C:\\Users\\Steve\\Desktop\\test.jpg";

But you don't actually need it here. Just do:

String command = "Outlook /a C:\\Users\\Steve\\Desktop\\test.jpg";

Upvotes: 1

try:

String command = "cmd /c start Outlook /a C:\\Users\\Steve\\Desktop\\test.jpg";

you forget to call cmd, it will call the command prompt which will execute your command..

Upvotes: 0

Related Questions