• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error while executing shell script from java

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic