How to execute VBS script in Java?
java, vbscript
Solution
VB scripts are typically executed using utility named `cscript`. I do not remember where this utility is located but it is definitely in path, so you can just run it directly like `cscript yourscript.vbs`. Now just use `Runtime.exec()` or `ProcessBuilder` from java.
And for your convenience avoid using back slashes in java code. Use forward slash instead. It works in windows perfectly and do not require duplications like `\\`.
Problem
How to execute VBS script in Java? What is he preferred way? I found in Internet so many advices, so I don't know what is better... 1. ``` Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); ``` 2. ``` Runtime.getRuntime().exec("wscript.exe " + file.getPath()) ``` 3. ``` String script = "C:\\work\\selenium\\chrome\\test.vbs"; String executable = "C:\\windows\\...\\vbs.exe"; String cmdArr [] = {executable, script}; Runtime.getRuntime ().exec (cmdArr); ``` 4. `Runtime.getRuntime().exec("cmd /c a.vbs");` 5. ``` Desktop#open(new File("c:/a.vbs")); ``` And It is not all. What kind of this to choose? I need to execute following script: ``` If Not IsObject(application) Then Set application = SapGuiAuto.GetScriptingEngine End If If Not IsObject(connection) Then Set connection = application.Children(0) End If If Not IsObject(session) Then Set session = connection.Children(0) End If If IsObject(WScript) Then WScript.ConnectObject session, "on" WScript.ConnectObject application, "on" ```