|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Problem running xterm ssh using Runtime.exec!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|