So I have a process (java commandline application) that is hidden with the output and input being redirected. I can read the output very easily and that works, but when I send a command it dosen't work.
I think that I have determined that the input is not being redirected because:
(A) When I send WriteLine(//command here); Flush
no command is acknowledged by the program
(B) When I unhide the cmd window (StartInfo.CreateNoWindow = false;
) I can enter commands and run them (in the cmd window) even though the StandardInput is being redirected (StartInfo.RedirectStandardInput = true;
)
Here is the code:
namespace bukkit
{
public partial class Form1 : Form
{
private static StringBuilder _txt = new StringBuilder();
private static bool _scrolled = false;
Process mncrft = new Process();
public Form1()
{
InitializeComponent();
mncrft.StartInfo.WorkingDirectory = Path.GetTempPath();
mncrft.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\mncrft\\mncrft.jar";
mncrft.StartInfo.FileName = "java.exe";
mncrft.StartInfo.UseShellExecute = false;
mncrft.StartInfo.RedirectStandardOutput = true;
mncrft.StartInfo.RedirectStandardError = true;
mncrft.StartInfo.RedirectStandardInput = true;
mncrft.StartInfo.CreateNoWindow = false;
mncrft.ErrorDataReceived += build_ErrorDataReceived;
//mncrft.OutputDataReceived += build_ErrorDataReceived;
mncrft.EnableRaisingEvents = true;
//mncrft.StandardInput.NewLine = "\r\n";
mncrft.Start();
mncrft.BeginOutputReadLine();
mncrft.BeginErrorReadLine();
}
private void Form1_Load(object sender, EventArgs e)
{
_txt.AppendLine("Starting Minecraft...");
}
private void Form1_Close(object sender, EventArgs e)
{
mncrft.Close();
}
static void build_ErrorDataReceived(object sender, DataReceivedEventArg开发者_高级运维s e)
{
string msg = e.Data;
if (msg != null && msg.Length > 0)
{
_txt.AppendLine(msg);
_scrolled = false;
}
}
private void mainTimer_Tick(object sender, EventArgs e)
{
if (_txt.Length > 0)
{
txtOutput.Text = _txt.ToString();
// scroll down
if (_scrolled == false)
{
txtOutput.SelectionStart = txtOutput.Text.Length;
txtOutput.ScrollToCaret();
_scrolled = true;
}
}
}
private void Execute_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
mncrft.StandardInput.WriteLine(textBox1.Text);
mncrft.StandardInput.Flush();
}
}
}
}
How can I redirect the input so that I can send commands?
Thanks, Adam
P.S: If this is confusing, just put a comment down and I will gladly clarify.
Answered
Thanks to Tim, Replace the following lines:
mncrft.StartInfo.FileName = "java.exe";
mncrft.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\mncrft\\mncrft.jar";
with:
mncrft.StartInfo.FileName = "java";
mncrft.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\mncrft\\bukkit.jar -nojline";
Ok, I was messing with this all day, and I found the answer. When you launch bukkit with your application you need to include '-nojline' to your process Arguments. This makes bukkit's inputs work correctly with the standardinput.
https://github.com/Bukkit/CraftBukkit/commit/22a44d47ac48fb65bb61fb823c84bff9494f5033
Adam, I dont know for sure the answer for your question, but I do have similar problem in the past and learned that winform application do behave slightly differently when it comes to stdin and stdout with console. I resolved my issue after reading some posts online using API call to AttachConsole (and FreeConsole to release it). Maybe it will be a good starting point to investigate into as a solution to your problem.
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
This is very weird because if the Input Stream didn't redirect (for some reason) then you would get an exception the moment you try to do anything with the Process.StandartInput property, and if it did really redirect then you wouldn't be able to enter commands in the cmd window and execute them !
If the problem is because you're making a WinForms application (as Fadrian mentioned) then try making it a WPF application (I personally prefer WPF over WinForms), if you never coded a WPF app before then download the code I linked in my blog post here (yes, the one you visited earlier) and modify it so that it loads your Bukkit Server instead of the regular Minecraft Server, if it didn't work then the problem might be with the Bukkit Server, at that point I recommend that you go to Bukkit's forum and ask there.
精彩评论