真的需要帮助!我想知道如何在VB 2008中将jar输出到文本框中。 另外我想向它发送命令(就像使用此命令时CMD一样: C:\ Windows \ System32 \ java.exe -Xms128M -Xmx1024M -jar Craftbukkit.jar)
答案 0 :(得分:1)
下面是一个概念验证。你必须根据自己的愿望和愿望调整它。这是做什么的:
同样,这段代码只是一个概念证明,它远未完成(但它足以证明)。你必须添加一些额外的支票等,使其“防水”。
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