1

I run the program like this:

    Process process;
    try {
        process = new ProcessBuilder("java", "-jar", "test.jar", "1", "20").start();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
          System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The program I call uses standard output System.out.println("Hello!"); However, the the calling program gets nothing back. Am I using ProcessBuilder() wrong? Thanks!

6
  • please show all code, how do you call this piece of code? Commented Nov 12, 2015 at 8:59
  • 1
    Are you 100% positive that `java -jar test.jar 1 20" outputs something? Have you tried to also redirect the error stream? Commented Nov 12, 2015 at 9:05
  • 1
    you can follow this example in stackoverflow clickhere Commented Nov 12, 2015 at 9:17
  • 1
    @ManojKrishna good hint, but preferably write your comment that way: "you can follow this example in stackoverflow" Commented Nov 12, 2015 at 9:19
  • 1
    Is there a constraint to run a second JVM? Why not running the code in the current JVM using another class loader? Commented Nov 12, 2015 at 10:01

1 Answer 1

1

If there is no constraint to start another JVM (for example: usage of System.exit() in test.jar) you could load and run the test.jar inside the current JVM.

Following snippet shows the principle.

File file = new File("/tmp/test.jar");
URLClassLoader loader = new URLClassLoader(
        new URL[]{file.toURI().toURL()}
);

String className = new JarFile(file)
        .getManifest()
        .getMainAttributes()
        .getValue(Attributes.Name.MAIN_CLASS);

Method main = loader
        .loadClass(className)
        .getDeclaredMethod("main", String[].class);

Object arg = new String[]{"1", "20"};

try {
    main.invoke(null, arg);
} catch (Exception e) {
    // do appropriate exception handling here
    e.printStackTrace(System.err);
}
Sign up to request clarification or add additional context in comments.

4 Comments

But could you capture the output of that jar without using processBuilder?
@firstpostcommenter You mean to redirect the output of test.jar to something else then System.out?
yes, this code doesn't allow to consume the System.out where as processBuilder makes it possible (We could use a shell script to pipe the first jar output to second jar input if required but that is different solution)
@firstpostcommenter It depends what are your requirements and what you want to achieve. Instead of starting another JVM from within the current JVM using a loggin framework would be a less memory consuming and probaly faster (related to the startup time for the additional JVM) solution.

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.