| Author |
Executing Unix command from java : problem
|
SN Hota
Greenhorn
Joined: Jun 20, 2008
Posts: 4
|
|
Hi All, Please check the below program while is not executing, when the unix command's having \&\& is used. The following command copies the src folder to dest folder with out data loss ... <CODE> import java.io.*; import java.util.*; public class Sample { public static void main(String[] args) { try { String ExCmd="ssh oradev@dev.excl.com cd /u01/11510/devdb/9.2.0/slax \\&\\& /bin/tar cf - . | ssh oratest@test.excl.com cd /d01/AppsTest/testappl2 \\&\\& /bin/tar -xf - > test.log"; System.out.println("Command to execute : "+ExCmd); Process SshProc = (Runtime.getRuntime()).exec(ExCmd); // any error message? StreamReader errorStream = new StreamReader(SshProc.getErrorStream(), "DEBUG"); // any output? StreamReader outputStream = new StreamReader(SshProc.getInputStream(), "OUTPUT"); // kick them off errorStream.start(); outputStream.start(); int pp =SshProc.waitFor(); System.out.println("command execution value: "+pp); }catch(Exception e) { e.printStackTrace(); } } } class StreamReader extends Thread { InputStream is; String type; StreamReader(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null) { System.out.println(type + ">" + line); } } catch (IOException ioe) { ioe.printStackTrace(); } } } </CODE> When this is executed iam getting process value as "0". But nothing is copying to target .. So to resolve this i have created a script .. and passed arguments to that script and chaged command as follows : <CODE> String ExCmd = "sh /home/juser/movedir.sh oradev@dev.excl.com /u01/11510/devdb/9.2.0/slax /bin/tar oratest@test.excl.com /d01/AppsTest/testappl2 /bin/tar /home/juser/test"; </CODE> Then it is executing fine. But in my scenario iam restricted to use scripts. So please can any body have a solution for this. Thanks, SN
|
Surya
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 3212
|
|
|
http://forum.java.sun.com/thread.jspa?threadID=5306948
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
The reason is simple. Mechanisms like || (execute the second program if the first fails), && (execute the second program if the first succeeds), < (redirect input stream), > (redirect output/error stream) and & (run in background) are interpreted by the SHELL. Java does not execute shell commands but separate commands. So here's a short way of how to implement them in Java: - ||: wait until the first program (Process) exits. Check the return code. If it is not 0, execute the second program. - &&: see || but execute if 0 - <: use the Process' getOutputStream - >: use the Process' getInputStream and getOutputStream - 2>&1 (redirect error to output): use ProcessBuilder - &: execute the program in a different thread
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
SN Hota
Greenhorn
Joined: Jun 20, 2008
Posts: 4
|
|
Thank you, very much can you please send me a sample code..? It will be helpful for me. Thanks in advance.
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
command1 || command2: command1 && command2: command < file: You can also use BufferedReader and PrintWriter for reading and writing. command > file Use getErrorStream to redirect the error stream instead of the output. If you want to redirect both to different files you'll need to redirect both. You'll need to do some threading for that. command > file 2>&1: As a final note, you should definitely read this article on JavaWorld.com.
|
 |
SN Hota
Greenhorn
Joined: Jun 20, 2008
Posts: 4
|
|
|
Thank you...for your quick reply..i will try this.
|
 |
 |
|
|
subject: Executing Unix command from java : problem
|
|
|