I got a problem when trying to redirect cmd output to a Java var.
My Source:
System.out.println("Init WAR packaging");
ProcessBuilder builder = new ProcessBuilder(Arrays.asList(new String[] {"cmd.exe", "/C", "start", "/wait", "new.bat"}));
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder strBuilder = new StringBuilder();
String line = null;
while (process.isAlive()) {
if((line = reader.readLine()) != null) {
strBuilder.append(line);
strBuilder.append(System.getProperty("line.separator"));
} else {
System.out.println("123");
Thread.sleep(10);
}
}
String result = strBuilder.toString();
System.out.println(result);
System.out.println("Start WAR packaging");
The Batch contains mainly:
jar -cvf test.war *.jsp *.xml
The output I get in the console:
Init WAR packaging
123
Start WAR packaging
So all i get as output from my CMD is null the actual CMD output looks like:
asdf.jsp wird hinzugefügt(ein = 17270) (aus = 4693)(72 % verkleinert)
qwer.jsp wird hinzugefügt(ein = 12969) (aus = 3519)(72 % verkleinert)
yxcv.jsp wird hinzugefügt(ein = 22463) (aus = 5375)(76 % verkleinert)
rewq.jsp wird hinzugefügt(ein = 30687) (aus = 6748)(78 % verkleinert)
jhgf.jsp wird hinzugefügt(ein = 47974) (aus = 11005)(77 % verkleinert)
I think thats actually the way it's ment to look like.
Really appreciate any kind of help as i couldn't get along with the information found on google / other stackoverflow Questions.
Thank you and have a nice day :)
EDIT:
I modified the whole code as suggested by arataj (hope i got you right):
ProcessBuilder builder = new ProcessBuilder(Arrays.asList(new String[] {"cmd.exe", "/C", "start", "/wait", "new.bat"}));
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder strBuilder = new StringBuilder();
String line = null;
while (process.isAlive()) {
Thread.sleep(10);
line = reader.readLine();
strBuilder.append(line);
strBuilder.append(System.getProperty("line.separator"));
}
line = reader.readLine();
strBuilder.append(line);
String result = strBuilder.toString();
System.out.println(result);
Console output:
Init WAR packaging
null
null
Start WAR packaging