Link to home
Start Free TrialLog in
Avatar of menakaindrani
menakaindrani

asked on

execute shell script from java

Hi am getting the following error.

public static void RunExe()
    {
          String st = "";
          st = "/bin/sh.exe C:/convert/runshell.sh";                                    
       try
       {
          Runtime.getRuntime().exec ( st ) ;//error here
       }
       catch(Exception ex)
       {
          ex.printStackTrace();
       }
    }

java.io.IOException: CreateProcess: /bin/sh.exe runshell.sh error=2
        at java.lang.Win32Process.create(Native Method)
        at java.lang.Win32Process.<init>(Unknown Source)
        at java.lang.Runtime.execInternal(Native Method)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at cal.Run.RunExe(Run.java:16)
        at cal.Run.main(Run.java:43)

what is the problem...
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(You need the right path to sh.exe)
Avatar of sciuriware
sciuriware

If you are on linux/unix, then

 String st = "/bin/sh.exe /convert/runshell.sh";

will work, provided that the script is in /convert      ...............
<*>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sh.exe should be in your path so you could try:
String st="sh.exe /convert/runshell.sh";
Again: only when /convert is on your current disk!
Test with FULL pathnames!
<*>
>>If you are on linux/unix, then

Unlikely with a .exe extension ;-)

>>sh.exe should be in your path

Why so?
Try this to verify everything:
public static void RunExe()
    {
         File sh=new File("/bin/sh.exe");
         File script=new File("c:/convert/runshell.sh")
        if (sh.exists()==false) {
            throw new FileNotFoundException("Command shell (sh.exe)");
        if (script.exists()==false) {
            throw new FileNotFoundException("Shell script(runshell.sh)");

       String st = sh..getAbsolutePath()+" "+script..getAbsolutePath();
       try
       {
          Runtime.getRuntime().exec ( st ) ;//error here
       }
       catch(Exception ex)
       {
          ex.printStackTrace();
       }
    }
getAbsolutePath is not necessary - both paths are already absolute
"/" means root of the drive where your java app is executed. Try copying java class to the drive where sh.exe is, and things should work fine. Otherwise you'll have to specify full path like "C:/.."