aspose file tools*
The moose likes Java in General and the fly likes Error while executing shell script from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Error while executing shell script from java" Watch "Error while executing shell script from java" New topic
Author

Error while executing shell script from java

Abhishek Mish
Greenhorn

Joined: Dec 21, 2007
Posts: 29
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
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
Running a Unix shell script Windows isn't going to work.
Abhishek Mish
Greenhorn

Joined: Dec 21, 2007
Posts: 29
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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error while executing shell script from java