java.lang.runtime.exec()

Discussion in 'Java' started by Mitschu, Dec 13, 2007.

  1. Mitschu

    Mitschu Guest

    Hello
    I'm using java.lang.runtime.exec() in Windows enviroment without
    problems. Now on a linux Server it won't run. Can you see an error?
    Has the string to be terminated with sth? I tried \r and \n without
    success.

    try {
    Runtime runtime = Runtime.getRuntime();
    Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );
    p.waitFor();
    out.println("Exit status: " + p.exitValue() );
    }catch(Exception e) {
    out.println("Error: " + e );
    }


    Thanks for any help.
    Michael
    Mitschu, Dec 13, 2007
    #1
    1. Advertising

  2. Mitschu wrote:
    > Hello
    > I'm using java.lang.runtime.exec() in Windows enviroment without
    > problems. Now on a linux Server it won't run. Can you see an error?
    > Has the string to be terminated with sth? I tried \r and \n without
    > success.
    >
    > try {
    > Runtime runtime = Runtime.getRuntime();
    > Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );
    > p.waitFor();
    > out.println("Exit status: " + p.exitValue() );
    > }catch(Exception e) {
    > out.println("Error: " + e );
    > }
    >
    >


    I've never used Runtime.exec() but from prior discussions here I vaguely
    recall that you need to feed it the name of a native executable. In your
    case, the name of a shell should be the first thing you give to exec().

    The API docs and/or Google Groups may well be your friends.

    If you really are using echo to append to a file, I suggest you use Java
    IO classes instead.
    RedGrittyBrick, Dec 13, 2007
    #2
    1. Advertising

  3. On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu wrote:
    > I'm using java.lang.runtime.exec() in Windows enviroment without
    > problems. Now on a linux Server it won't run. Can you see an error?
    > Has the string to be terminated with sth? I tried \r and \n without
    > success.
    >
    > try {
    > Runtime runtime = Runtime.getRuntime();
    > Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );


    The main problem is that you are using shell features (redirection)
    but Runtime.exec() does not invoke a shell.

    To RGB: Although echo is built into many shells it's also a real
    executable on Unix, so that isn't the reason a shell is necessary
    here.

    There may be additional problems with your *real* command if you are
    using quotation marks to group arguments, since the command line is
    tokenized without regard to quoting or escaping when you use
    exec(String). A better choice is usually exec(String[]).

    Try it like this:

    String[] cmd = { "/bin/sh", "-c", "echo 'tttt' >> /var/test/txt" };

    /gordon


    --
    Gordon Beaton, Dec 13, 2007
    #3
  4. Mitschu

    Nigel Wade Guest

    Mitschu wrote:

    > Hello
    > I'm using java.lang.runtime.exec() in Windows enviroment without
    > problems. Now on a linux Server it won't run. Can you see an error?
    > Has the string to be terminated with sth? I tried \r and \n without
    > success.
    >
    > try {
    > Runtime runtime = Runtime.getRuntime();


    This subject was done to death last week (or was it the week before). Please
    look through the Google groups archive for the comp.lang.java.* groups:

    <http://groups.google.co.uk/groups?&as_epq=runtime.exec&as_ugroup=comp.lang.java.*&as_drrb=b&as_mind=1&as_minm=12&as_miny=2007&as_maxd=13&as_maxm=12&as_maxy=2007>

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Dec 13, 2007
    #4
  5. Nigel Wade schreef:
    > Mitschu wrote:
    >
    >> Hello
    >> I'm using java.lang.runtime.exec() in Windows enviroment without
    >> problems. Now on a linux Server it won't run. Can you see an error?
    >> Has the string to be terminated with sth? I tried \r and \n without
    >> success.
    >>
    >> try {
    >> Runtime runtime = Runtime.getRuntime();

    >
    > This subject was done to death last week (or was it the week before). Please
    > look through the Google groups archive for the comp.lang.java.* groups:
    >
    > <http://groups.google.co.uk/groups?&as_epq=runtime.exec&as_ugroup=comp.lang.java.*&as_drrb=b&as_mind=1&as_minm=12&as_miny=2007&as_maxd=13&as_maxm=12&as_maxy=2007>


    Indeed, and the thread was started by me.

    In short:
    - use ProcessBuilder instead of Runtime.exec (Java 5+)
    - redirection will not work except if you invoke a shell and tell it to
    run the command
    - better read the output from the process and write it to a file
    yourself, in a separate thread (watch out for the names of the methods)

    Read http://mindprod.com/jgloss/exec.html.

    Much luck, H.
    --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.4-svn0 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD8DBQFHYWqae+7xMGD3itQRAtGMAJ9vVCXk2aQaIXkBTTNQXDkO9bu8VACfWVH0
    kx9+0MIVJJDnLW+dwC4FAe0=
    =f8MM
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Dec 13, 2007
    #5
  6. Mitschu

    Roedy Green Guest

    On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu
    <> wrote, quoted or indirectly quoted someone
    who said :

    > Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );


    See http://mindprod.com/jgloss/exec.html

    You are making the most common error. echo is not an executable. It
    is a command to the command interpreter, as is >> (at least it is in
    Windows, don't know about your platform).

    You must spawn a copy of a command interpreter, and feed it that line
    as a parameter.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Dec 13, 2007
    #6
  7. Roedy Green wrote:
    > On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    >> Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );

    >
    > See http://mindprod.com/jgloss/exec.html
    >
    > You are making the most common error. echo is not an executable.


    Probably one of the most common errors, but on my machine there is a
    /bin/echo. There is also a build in echo in my shell (bash). Does that
    mean I can both eat the cake and have it too? ;-)

    //Roger Lindsjö
    Roger Lindsjö, Dec 13, 2007
    #7
    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. Harald Kirsch
    Replies:
    4
    Views:
    2,809
    Harald Kirsch
    Aug 31, 2004
  2. Darren
    Replies:
    10
    Views:
    1,421
    Darren
    Nov 15, 2005
  3. seenuvas
    Replies:
    7
    Views:
    2,638
    Roedy Green
    Jan 24, 2006
  4. seenuvas
    Replies:
    0
    Views:
    505
    seenuvas
    Jan 24, 2006
  5. Hal Vaughan
    Replies:
    11
    Views:
    1,075
    Gordon Beaton
    May 22, 2006
Loading...

Share This Page