• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing Unix command from java : problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://forum.java.sun.com/thread.jspa?threadID=5306948
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
SN Hota
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, very much can you please send me a sample code..? It will be helpful for me.

Thanks in advance.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you...for your quick reply..i will try this.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic