首页 > 解决方案 > vb脚本提取可执行jar文件响应

问题描述

我有一个场景来执行一个可执行的 jar 文件,然后需要从命令提示符捕获响应。

Option Explicit
    Dim StrCmd,Path,processFile,str
    Path = "D:\modulefolder" 
    processFile = "java -jar xxx.jar"
    StrCmd = "CD /D "& Path & " & " & processFile &""
    Call Run(StrCmd,1,False)
    Function Run(StrCmd,Console,bWaitOnReturn)
        Dim ws,MyCmd,Result, str1

        Set ws = CreateObject("wscript.Shell")
    'A value of 0 to hide the MS-DOS console
        If Console = 0 Then
            MyCmd = "CMD /K " & StrCmd & ""
            Result = ws.run(MyCmd,Console,bWaitOnReturn)
    *some code using sendkeys to type in command prompt****

有人可以告诉我最后我应该如何从命令提示符中提取响应代码..

例如,我得到响应代码 201 表示通过,400 或 405 表示失败场景。

标签: vbscriptexecutable-jarhp-uft

解决方案


stdout将和的输出重定向stderr到一个文本文件并阅读:

java -jar myApp.jar >file.txt 2>&1

这会将 jar 文件的输出重定向到file.txt,您可以使用FileSystemObject

Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim File : Set File = Fso.OpenTextFile("Path/to/file.txt", 1)
Dim Text : Text = File.ReadAll

Text现在包含文件的内容作为变量供您根据需要使用。


推荐阅读