• Post Reply Bookmark Topic Watch Topic
  • New Topic

Execute linux Shell Command/script from Java returns 127  RSS feed

 
Krishnam Ch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I Am trying to execute shell script from java code through application URL/Servlet but it always returns code 127.
If same code executed directly by writing simple java class or calling directly /tomcat/UpdatePrice.sh in Linux is working fine.
Could you please suggest reason for this error.OS:Linux
Execute Shell Command/script from Java returns 127.
My code :
Process process = null;
try {
System.out.println("PrintTest.execute() -- ");
cmd[0] = "/bin/sh" ;
cmd[1] ="/tomcat/UpdatePrice.sh" ;
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
if (process != null && process.exitValue() != 0) {
//This is giving 127
System.out.println("Not Success -exit code "+process.exitValue());
}

} catch ( Exception e) {
System.out.println("Exceprion" );

}

Shell script : UpdatePrice.sh permission :777
#!/bin/sh
PATH=$PATH:/apps/Acrobat5/bin:
export PATH
echo "This will update Price Info"
exit 0
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishnam Ch wrote:I Am trying to execute shell script from java code through application URL/Servlet but it always returns code 127.
If same code executed directly by writing simple java class or calling directly /tomcat/UpdatePrice.sh in Linux is working fine.

127 is usually returned by the shell when a command executed by it is not found directly, or in any of the paths defined by PATH system environment variable; and since '/tomcat' isn't a normal directory (/usr/tomcat or /[s]bin/tomcat is more normal) I suspect it might be that.

Winston
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should read this article: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!