3

我在 MSDN中阅读了使用重定向输入和输出创建子进程。

而且我已经重定向了输出。但在我的情况下,重定向输入与此示例不同。

我已经java -jar xxx.jar在子进程中运行。并且将输入重定向到 stdinPipe 是成功的。但是我发现子进程 jar 不读取输入。

我已经像这样重定向了输入句柄:

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 

当我使用bSuccess = WriteFile(g_hChildStd_IN_Wr, pCmd, strlen(pCmd), &dwWritten, NULL);.It 在我用于输出和输入的小测试 jar 文件上运行System.out.println()完美System.in.read()

但是当我运行 jar 服务器时,输入无效并且重定向失败。我唯一知道的是 jar 使用。我ConsoleReader(System.in, System.out)不熟悉 java。所以有人能知道是什么禁止我重定向输入吗?

非常感谢!!

PS 源代码与开头给出的链接相同。


最后我发现jline.console.ConsoleReader.readLine(">",null)问题出在哪里。我用arg运行java,-nojline一切都很好!

我还没有阅读 jline.console.ConsoleReader 的源代码。我很高兴有人能告诉我 和 之间的System.in.read()区别jline.console.ConsoleReader.readLine(">",null)

问题代码如下:

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);
    }

因此,使用jline.console.ConsoleReader.readline()也不同于jline.console.ConsoleReader.readline(">",null).

我会继续研究。


jline源代码

readline() 函数等于 readline(null,null)。这意味着不会显示“>”提示,但根据第 493 行的源代码,其他提示相同。所以我怀疑这是我的 windows 操作系统(win8. 1)导致代码跑到if (!terminal.isSupported())代码中?还是我没看懂源代码?不熟悉java太难了。

4

0 回答 0