Runtime.getRuntime().exec

Discussion in 'Java' started by ilya, Apr 19, 2010.

  1. ilya

    ilya Guest

    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?
     
    ilya, Apr 19, 2010
    #1
    1. Advertising

  2. ilya

    Mike Amling Guest

    ilya wrote:
    > 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
     
    Mike Amling, Apr 19, 2010
    #2
    1. Advertising

  3. ok. Thank you for the explanations.
     
    Ilya Lakhmitski, Apr 19, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zsolt Koppany
    Replies:
    2
    Views:
    7,186
    SaintMagoo
    Mar 5, 2011
  2. MARTIN Herve \(EXT\)
    Replies:
    1
    Views:
    1,474
    Anthony Borla
    Nov 27, 2003
  3. paul brown
    Replies:
    3
    Views:
    5,595
    deepa gr
    Feb 17, 2009
  4. Andrew Regan

    Runtime.getRuntime().exec() ??

    Andrew Regan, Jun 28, 2004, in forum: Java
    Replies:
    3
    Views:
    912
    Yu SONG
    Jun 30, 2004
  5. S!mb@
    Replies:
    3
    Views:
    7,696
    Roedy Green
    Jul 19, 2004
Loading...

Share This Page