I am developing an application which needs to call some Jenkins instances, to do this I am using the jenkins-cli .jar
I could be calling the jar using the commandline to easily extract its output. Then however I need to parse exceptions myself.
To properly handle exceptions I am now invoking the main method of the jar via reflection:
URLClassLoader jenkinsClassloader = new URLClassLoader(new URL[]{"UrlToJenkins-Cli.jar"}, getClass().getClassLoader());
Class<?> jenkinsCli = Class.forName ("hudson.cli.CLI", false, jenkinsClassloader);
Method mainMethod = jenkinsCli.getDeclaredMethod("main", String[].class);
mainMethod.invoke (null, (Object) commandArray);
But now I need to somehow get the output the jar prints to System.out
I know I can use System.setOut()
. The problem there is that I have other threads logging simultaniously.
I read here that System.out is classloader specific, but I couldn't find any additional information about this.
Does someone know a way to get the stdout of the jar without it being interfered with from other threads?