3

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"

1 Answer 1

3

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 \\.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.