Running multiple Unix commands in Java

Discussion in 'Java' started by Jerome, Jan 24, 2005.

  1. Jerome

    Jerome Guest

    Hi there,

    I am trying to execute multiple unix commands in Java. i.e. cd into the
    directory, newline, and enter a command to run something.

    --In the terminal window
    $ cd xx/xx/xx/xx/
    $ runXX

    -- The Java
    String cmd = "/bash/sh cd/xx/xx/xx '\n' runXX";

    Runtime rt1 = Runtime.getRuntime();
    Process p1= rt1.exec("cmd");

    My java compiles but does not work with the string I have.

    Can anyone help.
    Thanks in advance.

    J.
     
    Jerome, Jan 24, 2005
    #1
    1. Advertising

  2. Jerome wrote:
    > String cmd = "/bash/sh cd/xx/xx/xx '\n' runXX";


    The first thing would be to get the Unix command right. The above won't
    even run from the command line. And Java will not magically fix your syntax.

    "man bash" should give you some information on how to run a command from
    a command line argument. Pay special attention to the "-c" option. And
    I would bet it is not "/bash/sh" but "/bin/sh" orn "/bin/bash" for a start.

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Jan 24, 2005
    #2
    1. Advertising

  3. On Mon, 24 Jan 2005 11:41:51 -0000, Jerome wrote:
    > I am trying to execute multiple unix commands in Java. i.e. cd into
    > the directory, newline, and enter a command to run something.


    In addition to Thomas' comments, consider that there is a version of
    Runtime.exec() that already does just that, i.e. lets you specify a
    directory to run the command in:

    File dir = new File("/home/gordon/slask");
    Process p = Runtime.getRuntime().exec("/bin/somecmd", null, dir);

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 24, 2005
    #3
  4. Jerome

    Juha Laiho Guest

    "Jerome" <> said:
    >I am trying to execute multiple unix commands in Java. i.e. cd into the
    >directory, newline, and enter a command to run something.
    >
    >--In the terminal window
    >$ cd xx/xx/xx/xx/
    >$ runXX
    >
    >-- The Java
    >String cmd = "/bash/sh cd/xx/xx/xx '\n' runXX";
    >
    >Runtime rt1 = Runtime.getRuntime();
    >Process p1= rt1.exec("cmd");


    If this really is your code (instead of just a typo), you just tried to run
    command named cmd (literally!), which may or may not exist.

    As to the syntax of 'String cmd', use semicolons instead newlines to
    separate commands.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, Jan 24, 2005
    #4
    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. Yadagiri Rao KP

    Run unix commands from Java

    Yadagiri Rao KP, Aug 5, 2003, in forum: Java
    Replies:
    2
    Views:
    7,849
    Dave Miller
    Aug 5, 2003
  2. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,995
    Thomas Stegen
    Jun 28, 2003
  3. gravi2020
    Replies:
    0
    Views:
    1,827
    gravi2020
    Jan 15, 2009
  4. Anonymous
    Replies:
    0
    Views:
    201
    Anonymous
    Apr 3, 2008
  5. Bruce Horrocks
    Replies:
    3
    Views:
    279
    Bruce Horrocks
    Jan 1, 2004
Loading...

Share This Page