0

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"

james
  • 1,031
  • 1
  • 15
  • 31
  • 1
    "java" doesnt look like a valid filename. Shouldnt it be "java.exe" or "javaw.exe" ? – Sam Axe Jan 24 '13 at 20:05
  • Java is fine, like typing cmd into Start -> Run instead of cmd.exe – james Jan 24 '13 at 20:06
  • and does "java" live in the working directory? Or in some other? perhaps specifying the full path to the "java" application. – Sam Axe Jan 24 '13 at 20:11
  • also, try calling `Flush()` on your streamwriter after the writeline call – Sam Axe Jan 24 '13 at 20:12
  • I did originally have it specifying the full path to java.exe but when I had this there wasn't any output at all. With just java I at least get an output. I actually just tried changed it to java.exe and it stopped outputting again. Not sure why, maybe its so the system decides which "java" to load – james Jan 24 '13 at 20:12
  • Just tried the flush, no luck. Thanks though that was one thing I didn't think of! – james Jan 24 '13 at 20:14

1 Answers1

0

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.

james
  • 1,031
  • 1
  • 15
  • 31