Reputation: 95
I'm working on an application to act as a 'wrapper' around another another application, in this case I'm working on a wrapper in java to go around a TF2 server. However, I'm running into a rather odd issue with getting the output from the console to appear. Even if I redirect the error stream so that it will go with the input stream, the output to console will cut off after a certain point. I can connect to the server in TF2, so I know for a fact that it's launched.
The code for the wrapper:
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);
}
}
It will read the output up until the second time this line appears: 'Calling BreakpadMiniDumpSystemInit',then no further console output appears, or atleast is picked up by the application.
Is there anything I can do to fix this or is that not possible?
Edit: My guess is it has something to do with the buffering, as trying to do this with python works fine.
Upvotes: 1
Views: 2078
Reputation: 1041
Instead of using BufferedReader use 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();
}
}
}
}
Upvotes: 1