Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 21st, 2003, 01:35 AM
Vijay55 Vijay55 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 4 Vijay55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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@192.168.0.33 || 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){
e.printStackTrace();
}

Any clues .. am i missing something

Reply With Quote
  #2  
Old April 21st, 2003, 08:24 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
This is the text from a Builder.com newsletter I received some time ago on this subject:
--------------------------
Code:
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 
native
programs from within Java breaks the platform independence principle, 
but
there is often a need to do this.

Here's an example of a simple class showing the UNIX ls command being
run:

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);
            System.out.print(loadStream(ps.getInputStream()));
            System.err.print(loadStream(ps.getErrorStream()));
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }

    // 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 ) {
            buffer.append((char)ptr);
        }
        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 
the
directory from which it is run.

What if you want to redirect those details into a file? The 
command-line
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 
ls
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 
argument
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 
command
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 
ls
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Problem running xterm ssh using Runtime.exec!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT