Runtime.getRuntime().exec

I

ilya

I try to execute the command: Runtime.getRuntime().exec("%ComSpec% /c
start calc") and java gives me the error:
java.io.IOException: Cannot run program "%ComSpec%": CreateProcess
error=2, The system can not find the file specified.

But if I execute in such way Runtime.getRuntime().exec("cmd /c start
calc"), all is ok.

What is the reason?
 
M

Mike Amling

ilya said:
I try to execute the command: Runtime.getRuntime().exec("%ComSpec% /c
start calc") and java gives me the error:
java.io.IOException: Cannot run program "%ComSpec%": CreateProcess
error=2, The system can not find the file specified.

But if I execute in such way Runtime.getRuntime().exec("cmd /c start
calc"), all is ok.

What is the reason?

It's the shell (cmd.exe) that substitutes values of environmental
variables for their percentified names. When you issue
exec("%ComSpec%..."), there is not yet any cmd.exe running that would
perform the substitution.
You could issue getenv("ComSpec") yourself and use the returned value
instead of "cmd".

--Mike Amling
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top