• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error while executing shell script from java

 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am trying to execute shell script from java, code scippet is below

public class ExecuteSh{
public static void main(String[] args){
try{
String[] cmd = new String[2];
cmd[0] = "/bin/sh";
cmd[1] = "D:\\loading\\test.sh";
String execString = "D:\\loading\\test.sh" ;
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
}
catch (Exception e){
e.printStackTrace();
}
}
}

I am getting below error
java.io.IOException: CreateProcess: \bin\sh D:\loading\test.sh error=3
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Sh.main(Sh.java:9)

I also tried changing the cmd array to only sh script name, but still it is not working.
In my local dev enviornment OS is Windows XP.

Kindly suggest,
Thanks
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running a Unix shell script Windows isn't going to work.
 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for responding..
I am able to run the script from Cygwin.
Is there any way where i can execute the shell script from java code in windows platform.

Thanks,
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn't you say you were using Cygwin?

In any case, the path "/bin" is meaningless outside of the Cygwin environment. Unless you're using a Cygwin Java, you'll need to use a real path.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic