我正在开发一个应用程序来充当“包装器”。围绕另一个应用程序,在这种情况下,我正在使用java中的包装器来绕过TF2服务器。但是,我从控制台出来的输出出现了一个相当奇怪的问题。即使我重定向错误流以使其与输入流一起使用,控制台的输出也会在某个点之后切断。我可以在TF2中连接到服务器,所以我知道它已经启动了。
包装器的代码:
public class TF2Wrapper {
public static void main(String[] args) throws IOException {
ProcessBuilder process = new ProcessBuilder("./srcds_run","+map pl_badwater","+port 27015","2>&1");
process.redirectErrorStream(true);
Process server = process.start();
BufferedReader inputStream = new BufferedReader(new InputStreamReader(server.getInputStream()));
String s;
while((s = inputStream.readLine()) != null)
{
System.out.println(s);
}
}
它会在第二次显示此行之前读取输出:'调用BreakpadMiniDumpSystemInit',然后不再显示控制台输出,或者至少应用程序拾取。
我能做些什么来解决这个问题,或者这是不可能的?
编辑:我的猜测是它与缓冲有关,因为尝试使用python工作正常。
答案 0 :(得分:0)
而不是使用BufferedReader使用ByteArrayOutputStream
public class Example {
public static void main(String[] args) throws IOException,
InterruptedException {
InputStream inputStream = null;
ByteArrayOutputStream arrayOutputStream = null;
//in case of window
ProcessBuilder builder = new ProcessBuilder("ipconfig");
try {
Process process = builder.start();
inputStream = process.getInputStream();
byte[] b = new byte[1024];
int size = 0;
arrayOutputStream = new ByteArrayOutputStream();
while ((size = inputStream.read(b)) != -1) {
arrayOutputStream.write(b, 0, size);
}
System.out.println(new String(arrayOutputStream.toByteArray()));
} catch (Exception e) {
e.getStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
if (arrayOutputStream != null)
arrayOutputStream.close();
} catch (Exception exception) {
exception.getStackTrace();
}
}
}
}