Results 1 to 8 of 8

Thread: Java execute shell commands?

  1. #1
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,162

    Java execute shell commands?

    What is the simplest way I could run bash scripts or shell commands using Java?

    I tried with exec, but it wont even accept the 'cd' command..

    Anyone know a simple way to do this? If I could just get it to run a bash script, I could put all my commands in there.

    Thanks

  2. #2
    Join Date
    Jul 2007
    Beans
    Hidden!
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Java execute shell commands?

    At a guess I would think that you execute the program 'bash' from java and pass it the commands you want executed in a file or pipe.
    ch
    In Switzerland we make it other
    with apologies to Gerard Hoffnung


  3. #3
    Join Date
    Oct 2007
    Location
    Vienna, Europe
    Beans
    77

    Re: Java execute shell commands?

    cd is not a separate program like ls or wc, but a command directly build into many command like interpreter (like bash).

    So Runtime#exec is the way to go, but you cannot change the working directory of the java process this way. What you can do is to use Runtime#exec to call a script that opens a shell and then execute a set of commands in that shell, including cd.

  4. #4
    Join Date
    May 2005
    Location
    USA
    Beans
    54

    Re: Java execute shell commands?

    import java.lang.* ;

    public class MyJavaClass
    {
    public void runCmd(String[] args)
    {
    String cmd = "/home_dir/./my_shell_script.sh" ;
    Runtime run = Runtime.getRuntime() ;
    Process pr = run.exec(cmd) ;
    pr.waitFor() ;
    BufferedReader buf = new BufferedReader( new InputStreamReader( pr.getInputStream() ) ) ;

    while ( ( String line ; line = buf.readLine() ) != null )
    {
    System.out.println(line) ;
    }

    }

    }
    ~kozmo

  5. #5
    Join Date
    Nov 2007
    Location
    New Zealand
    Beans
    1,026
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Java execute shell commands?

    Here is some code that does what you want

    http://ubuntuforums.org/showpost.php...1&postcount=19

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Beans
    2,185
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Java execute shell commands?

    A more flexible way than Runtime.exec(...) to do this is to use the class java.lang.ProcessBuilder. Look it up in the Java API documentation.

    *edit* Oh wait, who are we fooling? This is a 9 month old thread...
    Ubuntu 12.04

  7. #7
    Join Date
    Nov 2007
    Location
    New Zealand
    Beans
    1,026
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Java execute shell commands?

    Quote Originally Posted by jespdj View Post
    A more flexible way than Runtime.exec(...) to do this is to use the class java.lang.ProcessBuilder. Look it up in the Java API documentation.

    *edit* Oh wait, who are we fooling? This is a 9 month old thread...


    oops

  8. #8
    Join Date
    Dec 2007
    Beans
    1,061

    Re: Java execute shell commands?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •