I am using Runtime.getRuntime().exec() to run a shell script from java code.
String[] cmd = {"sh", "build.sh", "/Path/to my/sh file"};
try{
Process proc = Runtime.getRuntime().exec( cmd );
}
catch(Exception e){
System.out.println("Exception is:"+e);
}
It gives me the following output in console:
sh: Can't open build.sh
Am I following some wrong approach here? Cannot make out why his happens.
EDIT
Based on the comment here, I have modified the String[] cmd = {"sh", "build.sh", "/Path/to my/sh file"};
to String[] cmd = {"sh", "/Path/to my/sh file/build.sh", "/Path/to my/sh file"};
. Now the problem is this script need to be executed from a particular path. When I execute this script from command prompt, I first change the directory to that path and execute it. How should I modify this code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…