I am trying to invoke a shell script containing SSH command from a java program. However it is failing with a error code 1.
My java code is as:
public class CallScript {
private static String filePath="";
private static String args1="";
public static void main(String[] args) throws Exception{
if(args!=null && args.length > 0){
filePath = args[0];
if(args.length > 1){
args1=args[1];
}
}else{
throw new Exception("File Path should be first Argument");
}
System.out.println(args.length);
invokeScript(filePath,args1);
}
private static void invokeScript(String filePath, String arg1) throws Exception{
System.out.println("Inside invoke Script " + arg1);
Process p = Runtime.getRuntime().exec(filePath);
p.waitFor();
int exitVal = p.exitValue();
System.out.println("The Exit Value " + exitVal);
}
}
I compiled the program and placed the executable jar in my Unix environment.
shell script which is invoked from java
ssh -l >test.log
I used the following command to run my java program :
java -jar invokeScript.jar /tmp/upog/test.sh
output
Inside invoke Script
The Exit Value 1.
If I have some other command in the shell script like ls -al > test.log, the code is working with success and I am getting the return value 0.
Also if I invoke the shell script containing ssh command directly in Unix box, it is working fine.(the box have password-less connectivity)
But it is failing, if i call from java...
Any advice....
Runtime.getRuntime().exec(arg1);& notRuntime.getRuntime().exec(filePath);