3

I have read Creating a Child Process with Redirected Input and Output in MSDN.

And I have redirected output.But redirect input is different from this example in my case.

I have run java -jar xxx.jar in child process.And redirect the input to the stdinPipe is successful.But I found that child process , the jar , doesn't read the the input.

I have redirected the input handle like this:

ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO); 
siStartInfo.hStdError = g_hChildStd_OUT_Wr;
siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
siStartInfo.hStdInput = g_hChildStd_IN_Rd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

// Create the child process. 

bSuccess = CreateProcess(NULL, 
    szCmdline,     // command line which I use "java -jar xxx.jar"
    NULL,          // process security attributes 
    NULL,          // primary thread security attributes 
    TRUE,          // handles are inherited 
    0,             // creation flags 
    NULL,          // use parent's environment 
    NULL,          // use parent's current directory 
    &siStartInfo,  // STARTUPINFO pointer 
    &piProcInfo);  // receives PROCESS_INFORMATION 

When I using bSuccess = WriteFile(g_hChildStd_IN_Wr, pCmd, strlen(pCmd), &dwWritten, NULL);.It run perfect on my little test jar file which used System.out.println() and System.in.read() to output and input.

But when I run the jar server, the input is invalid and redirection is failed.The only thing I know is the jar use ConsoleReader(System.in, System.out).I'm not familiar with java.So can anyone know what have forbidden me to redirect the input?

Thanks a lot!!

P.S. The source code is same as the link given at beginning.


Finally I found that the jline.console.ConsoleReader.readLine(">",null) is the problem lies.I run java with arg -nojline and everything is all right!

I haven't read the jline.console.ConsoleReader's source code.I'm very glad anyone can tell my the difference between the System.in.read() and jline.console.ConsoleReader.readLine(">",null).

The problem code is below:

if (!useConsole) {
        return;
    }
    // CraftBukkit end

    jline.console.ConsoleReader bufferedreader = this.server.reader; // CraftBukkit
    String s;

    try {
        // CraftBukkit start - JLine disabling compatibility
        while (!this.server.isStopped() && this.server.isRunning()) {
            if (useJline) {
                s = bufferedreader.readLine(">", null);
            } else {
                s = bufferedreader.readLine();
            }
            if (s != null) {
                this.server.issueCommand(s, this.server);
            }
            // CraftBukkit end
        }
    } catch (IOException ioexception) {
        DedicatedServer.az().error("Exception handling console input", ioexception);
    }

So, using jline.console.ConsoleReader.readline() is also different from jline.console.ConsoleReader.readline(">",null).

I will continue research.


The jline source code

The readline() function equals to readline(null,null).Which means an ">" prompt won't be displayed but other is the same according to source code at line 493.So I doubt that is my windows os (win8.1) caused the code run into the if (!terminal.isSupported()) code ? Or I haven't understood the source code? Not familiar with java is so hard.

zzy
  • 1,771
  • 1
  • 13
  • 48

0 Answers0