我有一个需要从 .net 程序(VB)中执行的 java 命令行程序
java命令行程序在执行完成后会吐出一个我需要知道的数字。
我可以从 java 程序中获取 STDOUT 并将其读入 .net 程序吗?
编辑:我已经执行了程序,但是如何读取输入流?
这是执行java程序的代码
Dim processinfo As New ProcessStartInfo()
processinfo.WorkingDirectory = "C:\path\to\working\directory"
processinfo.FileName = "java.exe"
processinfo.Arguments = "-jar myprogram.jar argumentA argumentB"
Process.Start(processinfo)
这给我一个命令框,我可以看到程序执行..
编辑:有效的完整代码示例
Dim processinfo As New ProcessStartInfo()
processinfo.WorkingDirectory = "C:\"
processinfo.FileName = "java.exe"
processinfo.Arguments = "-jar my.jar list of params"
Dim myProcess As New Process()
processinfo.UseShellExecute = False
processinfo.RedirectStandardOutput = True
myProcess.StartInfo = processinfo
myProcess.Start()
Dim myStreamReader As StreamReader = myProcess.StandardOutput
' Read the standard output of the spawned process.
Dim myString As String
Do
myString = myStreamReader.ReadLine()
TextBox1.Text += myString
Loop Until (myStreamReader.EndOfStream)
myProcess.WaitForExit()
myProcess.Close()