Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing the output of a Java app initiated by a batch file on a remote server

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
I've got some code that launches a Java app (via a batch file) on a remote server then waits for it to finish. I need to capture the output of this Java app while it's running. Is there any way to to do this with what I currently have, or is there a better way to start and monitor this app?

The command executed by the batch file:
Code:
.\jre6\bin\java -jar JavaApp.jar gt

Code:
    Private Sub RunJavaAppFileGet()
        CreateProcess(strComputerName, "c:\Program Files (x86)\JavaApp\", """c:\Program Files (x86)\JavaApp\JavaApp_gt.bat""", strServiceUserName, strServicePassword)
    End Sub

    Private Sub CreateProcess(ByVal strComputer As String, ByVal strCurrentDirectory As String, ByVal strProcess As String, ByVal UserName As String, ByVal Password As String)
        Debug.Print("Process starting")

        processBatch = New ManagementClass("Win32_Process")
        inParams = processBatch.GetMethodParameters("Create")

        inParams("CurrentDirectory") = strCurrentDirectory
        inParams("CommandLine") = strProcess

        Dim co As ConnectionOptions = New ConnectionOptions()
        co.Username = UserName
        co.Password = Password

        Try
            If (strComputer = System.Environment.MachineName) Then
                msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2")
            Else
                msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
            End If

            msc.Connect()
            processBatch.Scope = msc

            Dim outParams As ManagementBaseObject = Nothing
            outParams = processBatch.InvokeMethod("Create", inParams, Nothing)

            WatchForProcessEnd(outParams("processID"))
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Function WatchForProcessEnd(processID As String) As ManagementEventWatcher
        Dim queryString As String = (Convert.ToString("SELECT TargetInstance" + "  FROM __InstanceDeletionEvent " + "WITHIN  10 " + " WHERE TargetInstance ISA 'Win32_Process' " + "   AND TargetInstance.ProcessID = '") & processID) + "'"
        Dim scope As String = "\\" & strComputerName & "\root\CIMV2"

        watcher = New ManagementEventWatcher(scope, queryString)
        AddHandler watcher.EventArrived, AddressOf ProcessEnded
        watcher.Start()
        Return watcher
    End Function

    Private Sub ProcessEnded(sender As Object, e As EventArrivedEventArgs)
        targetInstance = DirectCast(e.NewEvent.Properties("TargetInstance").Value, ManagementBaseObject)
        Dim processName As String = targetInstance.Properties("Name").Value.ToString()

        boolProcessEnded = True
        watcher.Stop()
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top