0 Replies - 1077 Views - Last Post: 02 April 2011 - 02:17 AM Rate Topic: -----

#1 killie01   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 05-November 10

Get the output from a java console and send input

Posted 02 April 2011 - 02:17 AM

Hi, i am making a interface for a java made console app,
the why is not the matter so don't go into that

it works like this:
consolebox.Text = "";
string otherpath = ServerPath;
string javapath;

if (Environment.Is64BitOperatingSystem)
{
	javapath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\java\jre6\bin\java.exe";
}
else
{
	javapath = "java";
}

craftbukkit.StartInfo.FileName = "cmd.exe";
craftbukkit.StartInfo.UseShellExecute = false;
craftbukkit.StartInfo.ErrorDialog = false;
craftbukkit.StartInfo.RedirectStandardInput = true;
craftbukkit.StartInfo.RedirectStandardOutput = true;
craftbukkit.StartInfo.RedirectStandardError = true;
craftbukkit.StartInfo.CreateNoWindow = true;
craftbukkit.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

#endregion

#region Startserver

craftbukkit.Start();
craftbukkit.StandardInput.WriteLine("cd " + ServerPath);
craftbukkit.StandardInput.WriteLine(javapath + " -Xms" + ram + "M -Xmx" + ram + "M" + " -jar " + "craftbukkit.jar");
OutputThread = new Thread(new ThreadStart(OutputUpdate));
OutputThread.IsBackground = true;
OutputThread.Start();
ErrorThread = new Thread(new ThreadStart(ErrorUpdate));
ErrorThread.IsBackground = true;
ErrorThread.Start();



this starts the process, redirects standardinput, standardoutput, standarderror
i get this output:
C:\Users\aron\Documents\Visual Studio 2010\Projects\CM\Debug>cd C:\Users\aron\Downloads\minecraft\Bukkit - Server
C:\Users\aron\Downloads\minecraft\Bukkit - Server>java -Xms986M -Xmx986M -jar craftbukkit.jar


but i don't get the output from the .jar i start
and input to the process doesn't work either:

private void SendButton_Click(object sender, RoutedEventArgs e)
{
	string cslinput = Sendbox.Text.ToString();
	if (isserverstart == 1)
	{
        craftbukkit.StandardInput.WriteLine(cslinput);
        consolebox.Text += "\n" + cslinput;
	}
	else
	{
		consolebox.Text += "\nServer is not started!";
	}

} 



and of course it checks for output every second:
while (!craftbukkit.StandardOutput.EndOfStream)
{
    if (craftbukkit.StandardOutput.ReadLine() == "")
    {
        this.Dispatcher.Invoke((Action)(() =>
        {
            consolebox.Text += "\n" + craftbukkit.StandardOutput.ReadLine();
            consolebox.ScrollToEnd();
        }));
    }
    Thread.Sleep(1000);
}



reason for the Dispatcher.Invoke: it is in a seperate Thread

This post has been edited by killie01: 02 April 2011 - 02:19 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1