I have created a Java application that has 2 Jar files. Jar1 is used to initialize and run Jar2, using this code :
Process process = runtime.exec( "java -jar Jar2.jar" );
printLogs( process );
.
.
.
private static boolean printLogs( Process process ) {
try {
BufferedInputStream logStream = new BufferedInputStream( process.getInputStream() );
String logs = "";
int buffer = 0;
while ( ( buffer = logStream.read() ) != -1 ) {
logs += (char)buffer;
}
if( !logs.isEmpty() ) logger.debug( logs );
} catch (IOException e) {}
return true;
}
I print many logs from Jar2 using Log4J, i.e.
logger.debug( "..." );
But none of the logs in Jar2 were printed to the console. I figured out it's because the logs are returned to Jar1 and not to the console, So i printed the returning stream using the above code. Logs are now printed fine but after all Jar2 process ends up, then all logs are printed at once in Jar1.
The question is: Can i print each log line in Jar2 in time instead of waiting all Jar2 process to end ?
Because Jar2 is a long process, and it is important that i can see those logs while the application is processing.