Results 1 to 4 of 4

Thread: Java runtime exec and pipe

  1. #1
    Join Date
    Dec 2009
    Beans
    454

    Java runtime exec and pipe

    Hi,

    I'm newbie of Java and I have a trouble on executing a command of shell where I use a pipe. I read on the web that it is possible to do something like this:
    Code:
    String[] cmd = {
    "/bin/sh",
    "-c",
    "ls /etc | grep release"
    };
    
    Process p = Runtime.getRuntime().exec(cmd);
    This works well. If I try something more complex like this
    Code:
    ssh  user@ip 'tar -jcf - file1 file2 ' | tar -C /path -jxf -
    it does not work and I receive the exit code 255. Do you have any idea?

    Thank you

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,302
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Java runtime exec and pipe

    Please show what you are putting in String[] cmd for this more complex form.

  3. #3
    Join Date
    Dec 2009
    Beans
    454

    Re: Java runtime exec and pipe

    I tried this
    Code:
    String[] cmd = {
    "/bin/sh",
    "-c",
    "ssh  user@ip 'tar -jcf - file1 file2 ' | tar -C /path -jxf -"
    };
    Process p = Runtime.getRuntime().exec(cmd);
    and also this
    Code:
    String cmd = "/bin/sh -c ssh  user@ip 'tar -jcf - file1 file2 ' | tar -C /path -jxf -";
    Process p = Runtime.getRuntime().exec(cmd);
    but both the solutions don't work.

  4. #4
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,302
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Java runtime exec and pipe

    The following works for me using authorized keys; I struggle to guess why it doesn't work for you. Does your command work from the command line? If tar or ssh weren't installed then you'd get an error!
    Code:
    import java.io.IOException;
    
    public class Call {
            
        public static void main(String[] args) throws IOException, InterruptedException {
    
    String[] cmd = {
    "/bin/sh",
    "-c",
    "ssh  me@myhost 'tar -jcf - file1 file2 ' | tar -C ./foo -jxf -"
    };
    
            Process proc = Runtime.getRuntime().exec(cmd);
            System.out.print("Exit code: ");
            System.out.println(proc.waitFor());
        }
    }

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
  •