Reputation: 2042
I have a VBS file test.vbs in C:/work/selenium/chrome/
and I want to run it from my Java program, so I tried this but with no luck:
public void test() throws InterruptedException {
Runtime rt = Runtime.getRuntime();
try {
Runtime.getRuntime().exec( "C:/work/selenium/chrome/test.vbs" );
}
catch( IOException e ) {
e.printStackTrace();
}
}
If I try to run some exe file with this method it runs well, but when I try to run a VBS file it says "not a valid win32 application".
Any idea how to run a VBS file from Java?
Upvotes: 10
Views: 49458
Reputation: 443
try {
Runtime.getRuntime().exec(new String[] {
"wscript.exe", "C:\\path\\example.vbs"
});
} catch (Exception ex) {
ex.printStackTrace();
}
You can use the code above to run vbs file.
Upvotes: 2
Reputation: 21
The complete code here
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class VBTest {
public static void main(String[] args) {
try {
String line;
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null;
Process process = Runtime.getRuntime().exec( "cscript E:/Send_Mail_updated.vbs" );
stdin = process.getOutputStream ();
stderr = process.getErrorStream ();
stdout = process.getInputStream ();
// "write" the parms into stdin
line = "param1" + "\n";
stdin.write(line.getBytes() );
stdin.flush();
line = "param2" + "\n";
stdin.write(line.getBytes() );
stdin.flush();
line = "param3" + "\n";
stdin.write(line.getBytes() );
stdin.flush();
stdin.close();
// clean up if any output in stdout
BufferedReader brCleanUp = new BufferedReader (new InputStreamReader (stdout));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stdout] " + line);
}
brCleanUp.close();
// clean up if any output in stderr
brCleanUp =
new BufferedReader (new InputStreamReader (stderr));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stderr] " + line);
}
brCleanUp.close();
}
catch( IOException e ) {
System.out.println(e);
//System.exit(0);
}
}
}
Upvotes: -1
Reputation: 169
public static void main(String[] args) {
try {
Runtime.getRuntime().exec( "wscript D:/Send_Mail_updated.vbs" );
}
catch( IOException e ) {
System.out.println(e);
System.exit(0);
}
}
This is working fine where Send_Mail_updated.vbs
is name of my VBS file
Upvotes: 14
Reputation: 36259
A vbs-Script isn't natively executable like a bat, cmd or exe-Program. You have to start the interpreter (vbs.exe?) and hand your script over as a parameter:
String script = "C:\\work\\selenium\\chrome\\test.vbs";
// search for real path:
String executable = "C:\\windows\\...\\vbs.exe";
String cmdArr [] = {executable, script};
Runtime.getRuntime ().exec (cmdArr);
Upvotes: 2