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.cre ate(Native Method)
at java.lang.Win32Process.<in it>(Unknow n Source)
at java.lang.Runtime.execInte rnal(Nativ e Method)
at java.lang.Runtime.exec(Unk nown Source)
at java.lang.Runtime.exec(Unk nown Source)
at java.lang.Runtime.exec(Unk nown Source)
at java.lang.Runtime.exec(Unk nown Source)
at cal.Run.RunExe(Run.java:16 )
at cal.Run.main(Run.java:43)
what is the problem...
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.cre
at java.lang.Win32Process.<in
at java.lang.Runtime.execInte
at java.lang.Runtime.exec(Unk
at java.lang.Runtime.exec(Unk
at java.lang.Runtime.exec(Unk
at java.lang.Runtime.exec(Unk
at cal.Run.RunExe(Run.java:16
at cal.Run.main(Run.java:43)
what is the problem...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(You need the right path to sh.exe)
If you are on linux/unix, then
String st = "/bin/sh.exe /convert/runshell.sh";
will work, provided that the script is in /convert ...............
<*>
String st = "/bin/sh.exe /convert/runshell.sh";
will work, provided that the script is in /convert ...............
<*>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sh.exe should be in your path so you could try:
String st="sh.exe /convert/runshell.sh";
String st="sh.exe /convert/runshell.sh";
Again: only when /convert is on your current disk!
Test with FULL pathnames!
<*>
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?
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("Com mand shell (sh.exe)");
if (script.exists()==false) {
throw new FileNotFoundException("She ll script(runshell.sh)");
String st = sh..getAbsolutePath()+" "+script..getAbsolutePath( );
try
{
Runtime.getRuntime().exec ( st ) ;//error here
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void RunExe()
{
File sh=new File("/bin/sh.exe");
File script=new File("c:/convert/runshell.
if (sh.exists()==false) {
throw new FileNotFoundException("Com
if (script.exists()==false) {
throw new FileNotFoundException("She
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:/.."