1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Rep Power

    Problem running xterm ssh using Runtime.exec!!

    I am not able to run the following command using Runtime.exec() but if the same command is executed in shell it gets executed.
    I am working on solais 8

    String toExecStr =
    "xterm -e /bin/sh -c \"ssh user@ || echo SSH failed.
    Press any key to quit.; read a \"";

    System.out.println("Running command :" + toExecStr);
    try {
    Process p = Runtime.getRuntime().exec(toExecStr);
    catch(Exception e){

    Any clues .. am i missing something
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Lincoln, NE. USA
    Rep Power
    This is the text from a Builder.com newsletter I received some time ago on this subject:
    Java has the ability to call native programs with the Runtime.exec
    method, but trying to call commands with redirection or piping will 
    fail. The
    solution is to run the command through the command shell. Calling 
    programs from within Java breaks the platform independence principle, 
    there is often a need to do this.
    Here's an example of a simple class showing the UNIX ls command being
    import java.io.BufferedInputStream;
    import java.io.IOException;
    public class ExecLs {
        static public void main(String[] args) {
            String cmd = "ls"
            try {
                Process ps = Runtime.getRuntime().exec(cmds);
            } catch(IOException ioe) {
        // read an input-stream into a String
        static String loadStream(InputStream in) throws IOException {
            int ptr = 0;
            in = new BufferedInputStream(in);
            StringBuffer buffer = new StringBuffer();
            while( (ptr = in.read()) != -1 ) {
            return buffer.toString();
    The important part of the code to note is the call to the exec method
    and the command String of ls. This program will output the details of 
    directory from which it is run.
    What if you want to redirect those details into a file? The 
    script to do this would be ls > FILE, but when you change the cmd
    variable to that, it fails with this error:
    /bin/ls: >: No such file or directory
    /bin/ls: FILE: No such file or directory
    It fails because the extra arguments are being passed straight to the 
    command and not to the actual command line. The solution is to break up
    the cmd string into a string array and pass the program you wish to run
    to the command shell.
    So change the cmd line to the following:
            String[] cmd = { "sh", "-c", "ls > FILE" };
    You'll get a file named FILE with the directory listing. The -c 
    tells it to read commands from the following string, and the final
    argument is the script you wish to run.
    Piping also works fine in this scenario, so you could change the 
    to the following:
            String[] cmd = { "/bin/sh", "-c", "/bin/ls | grep d > FILE" };
    This form would give you a file named FILE with only the entries from 
    that contained a d. Fully specifying the location of the sh and ls
    programs is a way to improve the security of your program.
    While using Runtime.exec is not the best way to create
    platform-independent Java, it's sometimes necessary. Using this 
    redirect technique helps
    to get around the limitations of Runtime.exec.

IMN logo majestic logo threadwatch logo seochat tools logo