I am working on a java program that runs a batch file using ProcessBuilder .
public class Test { public static void main(String[] args){ try { ProcessBuilder processBuilder = new ProcessBuilder("pathToMyBatch.bat"); Process process = processBuilder.start(); StreamReader fluxSortie = new StreamReader(process.getInputStream()); StreamReader fluxErreur = new StreamReader(process.getErrorStream()); new Thread(fluxSortie).start(); new Thread(fluxErreur).start(); } catch (IOException e) { e.printStackTrace(); } } static class StreamReader implements Runnable { private final InputStream inputStream; StreamReader(InputStream inputStream) { this.inputStream = inputStream; } private BufferedReader getBufferedReader(InputStream is) { return new BufferedReader(new InputStreamReader(is)); } @Override public void run() { BufferedReader br = getBufferedReader(inputStream); String ligne = ""; try { while ((ligne = br.readLine()) != null) { System.out.println(ligne); } } catch (IOException e) { e.printStackTrace(); } } } }
StreamReader class is a Runnable that waits for input and prints every line it StreamReader .
The thebatch file launches another Java application, just by calling java -classpath... and make another one related to it (and not related) to this application.
I can not change this party.
To make it clear: Program A runs the batch file B , which runs the application C
When I run the B command file directly from the windows, the C java application barely uses more than 2% of the processor. But when I run it through the java program A , C consumes 25% of the processor load (1 full core). In both cases, the charge for downloading a batch to the CPU is ~ 0%.
I assume that due to the weekends and flow errors of the Java application that are not being processed correctly.
I'm right? How can i fix this? Is there a way to get the process thread of a child process (child process)?
source share