I currently start a bunch of java processes from my WPF app using Process
, normally those would run in their own window. However when I want to send commands to the java app from my WPF app it requires me to run it with
StartInfo.UseShellExecute = false;
which causes the process to run without a console.
I've tried to look for answers here on SO (How to open/close console window dynamically from a wpf application?) and a few others but most seem to be for the app to be either ran in console mode or in UI mode. Nothing about attaching a console to a System.Diagnostics.Process
This version would open a Console
namespace MMSG.Instances
{
public abstract class JavaServer : Process
{
protected JavaServer(string workingDir)
{
StartInfo.FileName = "java";
StartInfo.UseShellExecute = true;
StartInfo.RedirectStandardInput = true;
StartInfo.WorkingDirectory = workingDir;
StartInfo.CreateNoWindow = false;
StartInfo.ErrorDialog = true;
EnableRaisingEvents = true;
}
protected JavaServer(string ram, string workingDir, string jarLocation) : this(workingDir)
{
StartInfo.Arguments = $"-Xms{ram} -Xmx{ram} -jar \"{jarLocation}\"";
}
}
}
however when trying to send input to the Process
using javaServer.StandardInput.WriteLine("stop");
I get this error: System.InvalidOperationException: The Process object must have the UseShellExecute property set to false in order to redirect IO streams.
And as stated before setting that to false removes the Console window altogether.
Expected result: allow sending of commands to the process while still having a Console window. Actual result: Either no commands or no Console