Problem / How to execute VBScript in Java using Runtime.getRuntime()
807603Oct 25 2007 — edited Oct 26 2007Dear Friends:
I am trying to excute a VBScript from a Java Program. For that I am making use of the Runtime.getRuntime() function in Java to execute the
VBScript from the shell.
The problem is I am not getting any error and neither is the script getting executed. It is returning immediately although I am making use of
proc.waitFor(); to wait till the time the VBScript finishes executing. If I execute the script separately from the command prompt, it takes more than
a minute to complete, but through this Java code it is returning immediately. The exit value I am getting is 0 , ie. no error! Here is the code:
---------
String cmd = "cscript.exe listprocesses-silent.vbs";
File filedir = new File("F:\\WMI\\myscripts");
String [] envp = new String[1];
envp[0] = "PATH="+
"C:\\WINDOWS\\system32;C:\\WINDOWS;"+"C:\\Progra~1\\Java\\jdk1.5.0_06\\bin";
try {
Runtime rt = Runtime.getRuntime();
Process proc;
proc = rt.exec(cmd,envp,filedir);
int exitVal = proc.waitFor();
System.out.println("Exit value="+exitVal);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
-------------
The VBScript is self contained and does not expect any input from the command prompt. I am invoking cscript.exe to execute the VBScript and it works like a
charm from the command prompt but using getRuntime this thing doesnot seem to work.
What am I doing wrong here ?
Is there a better way to execute a VBScript from a Java Code ? please help. thanks.