0

I have a project that uses ProcessBuilder to capture the output of the command "java -jar someJar.jar -argument", but have now moved the jar's source files to a separate package; somepackage. The package has a main function, so I would like to create a ProcessBuilder that captures the output of that process, as if it were a different Thread.

Is this possible, or will I have to completely re-write the code to allow it to use the source files instead of the binary?

Weston Reed
  • 187
  • 2
  • 9
  • You are mixing the terms in a confusing way. What do you mean with “have now moved the jar's source files to a separate package”? And why does this raise the desire to run “source files instead of the binary” (which is, of course, impossible without compiling them)? – Holger Feb 28 '17 at 15:31

1 Answers1

0

If I'm assuming right, the package has main function and we are trying to get output of the main method that executes with java -jar processbuilder command.

ProcessBuilder pb = new ProcessBuilder(your java -jar command);     
Process process = pb .start();
process.waitFor();
BufferedInputStream in = new BufferedInputStream(process.getInputStream());
byte[] contents = new byte[1024];
int jwtOytputBytesRead = 0;
String Output = "";
while ((jwtOytputBytesRead = in.read(contents)) != -1) {
            Output += new String(contents, 0, jwtOytputBytesRead);
    }
System.out.println(Output);

Try this link as well to specify the main the class Run class in Jar file

Community
  • 1
  • 1
  • Yes, that's what I'm trying to do, but it needs to be threaded, because I need to constantly be getting the output from it. – Weston Reed Feb 23 '17 at 03:08
  • how about Trying callable in java. Example: public class ProcessBuilderProcessor implements Callable{} Then use executor ProcessBuilderProcessor pbp=new ProcessBuilderProcessor(""); ExecutorService executr = Executors.newFixedThreadPool(1); Future future1 = executr.submit(pbp); – HaroonIsmailbasha Feb 23 '17 at 04:37