如何在 Java 中执行 VBS 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13586213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:18:38  来源:igfitidea点击:

How to execute VBS script in Java?

javavbscript

提问by MyTitle

How to execute VBS script in Java? What is he preferred way? I found in Internet so manyadvices, so I don't know what is better...

如何在 Java 中执行 VBS 脚本?他的首选方式是什么?我在网上找到了很多建议,所以我不知道哪个更好......

1.

1.

  Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());

2.

2.

Runtime.getRuntime().exec("wscript.exe " + file.getPath())

3.

3.

String script = "C:\work\selenium\chrome\test.vbs";
String executable = "C:\windows\...\vbs.exe"; 
String cmdArr [] = {executable, script};
Runtime.getRuntime ().exec (cmdArr);

4.

4.

Runtime.getRuntime().exec("cmd /c a.vbs");

Runtime.getRuntime().exec("cmd /c a.vbs");

5.

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"

回答by AlexR

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 ProcessBuilderfrom java.

VB 脚本通常使用名为cscript. 我不记得这个实用程序的位置,但它肯定在路径中,所以你可以像cscript yourscript.vbs. 现在只需使用Runtime.exec()ProcessBuilder来自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 \\.

为方便起见,请避免在 Java 代码中使用反斜杠。请改用正斜杠。它在 Windows 中完美运行,不需要像\\.