I'm currently trying to receive a feed from the java app minecraft_sever.jar.
This is what I am currently doing:
Dim mc_server As New Process Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mc_server.StartInfo.UseShellExecute = False
mc_server.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
mc_server.StartInfo.RedirectStandardOutput = True
mc_server.StartInfo.RedirectStandardInput = True
mc_server.StartInfo.CreateNoWindow = True
mc_server.StartInfo.FileName = "C:\Program Files (x86)\Java\jre6\bin\java.exe"
mc_server.StartInfo.Arguments = "java -Xmx1024M -Xms1024M -jar C:\Other\Minecraft\minecraft_server.jar nogui"
While (mc_server.HasExited = False)
Dim sLine As String = mc_server.StandardOutput.ReadLine
If (Not String.IsNullOrEmpty(sLine)) Then
Sadly, it's not working, and I have no clue why. Could anybody please help me?
Add event handler (see code example), call BeginOutputReadLine to start receiving. Also you should set SynchronizingObject property.
Actually you can set up most of this object in designer by adding it to form from Toolbox, and configure properties and events as any other component. Doing that would also automatically set SynchronizingObject property for you.
Start with finding the Process component in Toolbox and add it to your form.
Use Properties window to configure all properties, also set EnableRaisingEvents property. In same window switch to Events and doubleclick the OutputDataReceived event.
The only code you need to write is to start the process and start reading output, for example:
Thank you so much! I finally got it, added the process and everything and did everything correctly... but now there is another weird thing. Everytime I start it from my application it does everything like it's supposed to, but all it does is printing these lines:
Those are the two lines the Minecraft Sevrer always outputs, but then it continues and in my application it stopped. I started the server from cmd also too, with this line:
java.exe C:\Minecraft\minecraft_server.jar nogui And it printed everything correctly and the server started. Now what I don't know is why in my application it's not outputting the rest?
Now I have also noticed another weird thing, in the Debug Folder are now all the server.properties, the world files and everything but it should actually be in C:\Other\Minecraft\minecraft_server.jar, right? Because that's the argument I have in my process.
I deleted your attachment because it contained binary files. You can upload project source files only (without the Bin and Obj folders), if you find it necessary to provide more than you could post in a code box or a few here.
The other lines are outputted to StandardError. You can redirect that as well and set up the event handler for ErrorDataReceived event just like the OutputDataReceived event. Actually, if you don't care where the output comes from you can use the same event handler for both, just add to the handles list or in properties window select the same event handler for ErrorDataReceived also.
thank you so much. Now, I added the EventHandler for ErrorOutput to my OutputDataReceived, which looks like this:
Private Sub JavaProcess_OutputDataReceived(ByVal sender As System.Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles JavaProcess.OutputDataReceived, JavaProcess.ErrorDataReceived If e.Data = String.Empty Then Return
And also set the Property for "ErrorDataReceviedOutput" to true now too. But now nothing is being printed in my ListBox. Weird.
To begin reading error output you have to call BeginErrorReadLine, just like the corresponding BeginOutputReadLine for StandardOutput. Don't be afraid to read the relevant documentation when you're in pursuit of using a class.
John, thank you so much for your help. I really appreciate it. It finally worked!
Quick question: When I know stop the server using the in-server command "/stop" and in VB "JavaProcess.kill", the process (in TaskManager) is killed. but now when I try to restart (like click on "Restart") I get this:
An async read operation has already been started on the stream.