Process.StandardInput.WriteLine Not working on Java.exe

◇◆丶佛笑我妖孽 提交于 2020-01-14 06:10:10

问题


I'm having an issue with the following code:

private void Form1_Load(object sender, EventArgs e)
{
    cmdOutput = new StringBuilder("");
    cmdProcess = new Process();

    cmdProcess.StartInfo.WorkingDirectory = @"C:\android-sdk\tools";
    cmdProcess.StartInfo.FileName = @"java";
    cmdProcess.StartInfo.Arguments = @"-Xmx512m -Djava.ext.dirs=lib\;lib\x86_64 -Dcom.android.monkeyrunner.bindir=..\framework -jar lib\monkeyrunner.jar";

    cmdProcess.StartInfo.UseShellExecute = false;
    cmdProcess.StartInfo.CreateNoWindow = true;
    cmdProcess.StartInfo.RedirectStandardOutput = true;
    cmdProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
    cmdProcess.StartInfo.RedirectStandardInput = true;


    cmdProcess.Start();

    cmdStreamWriter = cmdProcess.StandardInput;
    cmdProcess.BeginOutputReadLine();

    // Even if i fire this later it doesn't work. 
    cmdStreamWriter.WriteLine(@"print 'Hello World'");
}

The issue is that:

cmdStreamWriter.WriteLine(@"print 'Hello World'"); 

Is not doing anything. Nothing is being written to the java process.

The output appears to be working fine (tested by loading a script directly to monkeyrunner.jar. But after trying many times I'm not getting any input.

This does work fine if I change the process to "cmd"


回答1:


I have managed to solve the issue:

From another issue I had I worked out that Jline (A java based command line extension) was being used. After some Googling I found that starting the Java app with:

 cmdProcess.StartInfo.Arguments = @"-Xmx512m -Djava.ext.dirs=lib\;lib\x86_64 -Dcom.android.monkeyrunner.bindir=..\framework 
-Djline.terminal=jline.UnsupportedTerminal -jar lib\monkeyrunner.jar";

The Amendment Being:

-Djline.terminal=jline.UnsupportedTerminal

This stopped Jline being loaded and allowed the standard input to work correctly again.

More information on -Djline.terminal argument can be found here:

http://jline.sourceforge.net/ - Installation.



来源:https://stackoverflow.com/questions/14509439/process-standardinput-writeline-not-working-on-java-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!