我的代码中有流程类,当我尝试从inputbuffer中读取大量数据时,它无法读取inputbufferreader数据,而流程类的waitfor方法从不返回任何内容


此问题发生在实时服务器中
但下面的代码运行正常,并从本地服务器中的inputbuffer读取所有数据

private static JSONObject ExecJniApp(String inputJsonString) throws JniException
{
int exitStatus = 0;
String workingDirectory = JniSettings.getJniAppDirectory();
String command = workingDirectory + binaryName;
System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
String outputString = "";
try
{
    Process p = Runtime.getRuntime().exec(command, null, new File(workingDirectory));
    // send input vi stdin
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
    writer.write("\n>>>>>>>>>>");
    writer.write(inputJsonString);
    writer.write("<<<<<<<<<<\n");
    writer.flush();
    if (JniSettings.isLinux())
    {
        p.waitFor();
    }
    else
    {
        // System.out.println("ExecJniApp: windows wait for");
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    for (;;)
    {
        String line = reader.readLine();
        if (line == null)
        {
            break;
        }
        // System.out.println(line);
        outputString += line + "\n";
    }
    // exit code of command and log error detail
    // exit status = 0 -> Success
    exitStatus = p.exitValue();
    if (exitStatus != 0)
    {
        System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
        System.out.println("ExecJniApp: outputString:\n" + outputString);
        throw new Exception("Exit status other than zero :- " + exitStatus + "\noutput: ");
    }
}
catch (Exception e)
{
    // System.out.println("ExecJniApp failed");
    e.printStackTrace();
    throw new JniException("JniInterface failed");
}
System.out.println("ExecJniApp: outputString:\n" + outputString);
int index1 = outputString.indexOf(">>>>>>>>>>");
if (index1 == -1)
{
    // System.out.println("ExecJniApp failed, Invalid output format");
    throw new JniException("ExecJniApp failed, Invalid output format");
}
index1 += 10;
int index2 = outputString.indexOf("<<<<<<<<<<");
if (index2 == -1 || index2 <= index1)
{
    // System.out.println("ExecJniApp failed, Invalid output format");
    throw new JniException("ExecJniApp failed, Invalid output format");
}
String outputJsonString = outputString.substring(index1, index2);
// System.out.println("ExecJniApp: outputJsonString: " + outputJsonString);
JSONParser parser = new JSONParser();
JSONObject obj = null;
try
{
    obj = (JSONObject) parser.parse(outputJsonString);
}
catch (ParseException e)
{
    // System.out.println("ExecJniApp failed, Json parse failed");
    e.printStackTrace();
    throw new JniException("JniInterface failed, Json parse failed");
}
Object errorObject = obj.get("error");
if (errorObject != null)
{
    String errorString = errorObject.toString();
    // System.out.println("ExecJniApp failed, " + errorString);
    throw new JniException("ExecJniApp failed, " + errorString);
}
return obj;
}

最佳答案

只需将您的代码替换为以下代码即可。
必须关闭您的InputStream连接。

 Process p = Runtime.getRuntime().exec(command, null, new File(workingDirectory));
// send input vi stdin
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("\n>>>>>>>>>>");
writer.write(inputJsonString);
writer.write("<<<<<<<<<<\n");
writer.flush();

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (;;)
{
    String line = reader.readLine();
    if (line == null)
    {
        break;
    }
    // System.out.println(line);
    outputString += line + "\n";
}
 p.getOutputStream().close();
 p.getInputStream().close();
}
catch(Exception e)
{
   e.printStackTrace();
}
if (JniSettings.isLinux())
{
    p.waitFor();
}
else
{
    // System.out.println("ExecJniApp: windows wait for");
}
// exit code of command and log error detail
// exit status = 0 -> Success
exitStatus = p.exitValue();
if (exitStatus != 0)
{
    System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
    System.out.println("ExecJniApp: outputString:\n" + outputString);
    throw new Exception("Exit status other than zero :- " + exitStatus + "\noutput: ");
}

09-27 19:04