如何在Java中执行VBS脚本?

时间:2012-11-27 14:19:06

标签: java vbscript

如何在Java中执行VBS脚本?他最喜欢的方式是什么?我在Internet 中找到了这么多的建议,所以我不知道什么是更好的......

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"));

并非全部。

选择什么样的?我需要执行以下脚本:

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 个答案:

答案 0 :(得分:3)

VB脚本通常使用名为cscript的实用程序执行。我不记得这个实用程序的位置,但它肯定在路径中,因此您可以像cscript yourscript.vbs一样直接运行它。现在只需使用java中的Runtime.exec()ProcessBuilder

为方便起见,请避免在java代码中使用反斜杠。请改用斜杠。它完美地适用于Windows,不需要重复\\