等待进程方法 class 不 return
wait for method of process class doesn't return
我的代码中有 Process Class,当我尝试从 inputbuffer 和 waitfor process class 方法读取大量数据时,它无法读取 inputbufferreader 数据从不 return 任何东西
- 此问题发生在实时服务器中
但下面的代码 运行 很好,并从本地服务器的输入缓冲区读取所有数据
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: ");
}
我的代码中有 Process Class,当我尝试从 inputbuffer 和 waitfor process class 方法读取大量数据时,它无法读取 inputbufferreader 数据从不 return 任何东西
- 此问题发生在实时服务器中
但下面的代码 运行 很好,并从本地服务器的输入缓冲区读取所有数据
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: ");
}