在vb中获取Jar输出并发送命令

时间:2013-08-25 05:48:24

标签: vb.net jar

真的需要帮助!我想知道如何在VB 2008中将jar输出到文本框中。 另外我想向它发送命令(就像使用此命令时CMD一样: C:\ Windows \ System32 \ java.exe -Xms128M -Xmx1024M -jar Craftbukkit.jar)

1 个答案:

答案 0 :(得分:1)

下面是一个概念验证。你必须根据自己的愿望和愿望调整它。这是做什么的:

  1. 在后台启动流程
  2. 开始阅读输出和错误流。
  3. 您可以通过文本框发送命令(并单击按钮)。
  4. 将输出/错误写入输出文本框。
  5. 同样,这段代码只是一个概念证明,它远未完成(但它足以证明)。你必须添加一些额外的支票等,使其“防水”。

    Public Class MyForm
        Private WithEvents _CmdProcess As Process
        Private Delegate Sub DisplayTextDelegate(text As String)
    
        Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim processInfo As New ProcessStartInfo()
            processInfo.FileName = "cmd"
            processInfo.RedirectStandardError = True
            processInfo.RedirectStandardInput = True
            processInfo.RedirectStandardOutput = True
            processInfo.UseShellExecute = False
            processInfo.CreateNoWindow = True
    
            _CmdProcess = Process.Start(processInfo)
            _CmdProcess.BeginOutputReadLine()
            _CmdProcess.BeginErrorReadLine()
        End Sub
    
        Private Sub MyForm_Disposed(sender As System.Object, e As System.EventArgs) Handles MyBase.Disposed
            If _CmdProcess IsNot Nothing Then
                _CmdProcess.Close()
            End If
            _CmdProcess = Nothing
        End Sub
    
        Private Sub btnExecute_Click(sender As System.Object, e As System.EventArgs) Handles btnExecute.Click
            If Not String.IsNullOrWhiteSpace(txtCommand.Text) Then
                Dim inputStream As System.IO.StreamWriter = _CmdProcess.StandardInput
                inputStream.WriteLine(txtCommand.Text)
                inputStream.Flush()
            End If
        End Sub
    
        Private Sub CmdProcess_ErrorDataReceived(sender As System.Object, e As System.Diagnostics.DataReceivedEventArgs) Handles _CmdProcess.ErrorDataReceived
            Invoke(New DisplayTextDelegate(AddressOf DisplayText), Environment.NewLine)
            Invoke(New DisplayTextDelegate(AddressOf DisplayText), "Error!")
            Invoke(New DisplayTextDelegate(AddressOf DisplayText), e.Data)
        End Sub
    
        Private Sub CmdProcess_OutputDataReceived(sender As System.Object, e As System.Diagnostics.DataReceivedEventArgs) Handles _CmdProcess.OutputDataReceived
            Invoke(New DisplayTextDelegate(AddressOf DisplayText), e.Data)
        End Sub
    
        Private Sub DisplayText(text As String)
            txtOutput.AppendText(Environment.NewLine)
            txtOutput.AppendText(text)
        End Sub
    End Class