-1

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
1
  • can't get why someone downvotes this Commented Apr 25, 2017 at 6:42

1 Answer 1

2

Solved the problem, the problem was that i did not have access to the output i wanted 'cause it was cause by a command calling another comman.

The way i solved it in code:

ProcessBuilder builder = new ProcessBuilder("cmd.exe");
Process  process = builder.start();
BufferedWriter out = new BufferedWriter(new 
OutputStreamWriter(process.getOutputStream()));
InputStream stdout = process.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));

out.write("start /b new.bat\n");
out.flush();
out.close();

Scanner scanner = new Scanner(stdout);
while (scanner.hasNextLine()) {
    System.out.println(scanner.nextLine());
}
System.out.println("end");
scanner.close();

Awnser based on this .

Thanks for the help!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.