如何在Java中执行VBS脚本?

如何在Java中执行VBS脚本?,java,vbscript,Java,Vbscript,如何在Java中执行VBS脚本?他喜欢哪种方式?我在网上找到了很多建议,所以我不知道什么更好 一, 二, 三, 四, Runtime.getRuntime().exec(“cmd/c a.vbs”) 五, 但这并不是全部 该选择哪种?我需要执行以下脚本: If Not IsObject(application) Then Set application = SapGuiAuto.GetScriptingEngine End If If Not IsObject(connection) Then

如何在Java中执行VBS脚本?他喜欢哪种方式?我在网上找到了很多建议,所以我不知道什么更好

一,

二,

三,

四,

Runtime.getRuntime().exec(“cmd/c a.vbs”)

五,

但这并不是全部

该选择哪种?我需要执行以下脚本:

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"

VB脚本通常使用名为
cscript
的实用程序执行。我不记得该实用程序位于何处,但它肯定位于path中,所以您可以像
cscript yourscript.vbs
那样直接运行它。现在只需使用java中的
Runtime.exec()
ProcessBuilder

为了方便起见,避免在java代码中使用反斜杠。改为使用正斜杠。它可以在windows中完美工作,不需要像
\\
这样的复制

Runtime.getRuntime().exec("wscript.exe " + file.getPath())
String script = "C:\\work\\selenium\\chrome\\test.vbs";
String executable = "C:\\windows\\...\\vbs.exe"; 
String cmdArr [] = {executable, script};
Runtime.getRuntime ().exec (cmdArr);
Desktop#open(new File("c:/a.vbs"));
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"