0

I have a jar created by maven with all dependencies in it. I have another java code that wants to use the jar as a library. My java code and the classes in the jar belong to two different packages.

The following shows how I am running the main class in the jar:

java -Dlog4j.configurationFile=src/test/resources/log4j2.xml 
     -cp pathtojars com.mainclass "parameters to mainclass" "inputfile"

How can i invoke the main class in the jar (equivalent of the above command) and get its STDOUT as a variable after execution is complete in my java code?

user720694
  • 2,035
  • 6
  • 35
  • 57
  • Ok, and what is the question – Dici Jul 27 '15 at 20:13
  • @Dici Question updated. – user720694 Jul 27 '15 at 20:16
  • Before trying to answer you, I have a question. Why should you run this Java code as an external application whereas your code is in Java ? Usually, you do this to interface a program written with another language. – Dici Jul 27 '15 at 20:18
  • 1
    possible duplicate of [Execute another jar in a java program](http://stackoverflow.com/questions/1320476/execute-another-jar-in-a-java-program) – hzpz Jul 27 '15 at 20:21
  • You can get more idea from this http://stackoverflow.com/questions/14165517/processbuilder-forwarding-stdout-and-stderr-of-started-processes-without-blocki – Acewin Jul 27 '15 at 21:23
  • @Dici How else can i invoke the main of a java class in a jar from another jar? – user720694 Jul 27 '15 at 23:53
  • @Karan what stopped you from using my answer ? – Dici Jul 28 '15 at 00:10
  • @Dici I was just answering the question you had above about why should i run this java code as an external application. – user720694 Jul 28 '15 at 00:23
  • @Karan Ok, I just meant that calling Java code from a Java code is a pretty common thing to do :D Usually to use classes in a jar you just add the jar to your classpath and then write your code ! – Dici Jul 28 '15 at 19:09

2 Answers2

1

You can use a ProcessBuilder to do this kind of things, but whether or not you should call a Java program from another Java program is another question :

Process process = new ProcessBuilder("java", "-Dlog4j.configurationFile=src/test/resources/log4j2.xml", "-cp", "pathtojars com.mainclass", "\"parameters to mainclass\"", "\"inputfile\"").start();
OutputStream os = process.getOutputStream();
Dici
  • 25,226
  • 7
  • 41
  • 82
  • So the requirement is that i need to package the calling class also into a jar. Then when i run this main jar which in turn invokes the library jar, does it have to be placed in a certain location to be able to call the library jar? – user720694 Jul 27 '15 at 20:42
  • Not really, as long as you set the working directory of the `ProcessBuilder` to the right place – Dici Jul 27 '15 at 20:43
  • Ass dici mentioned what you are trying to read is output (console output) of a process execution – Acewin Jul 27 '15 at 20:53
  • @Acewin You should post your own answer instead of editing mine – Dici Jul 27 '15 at 21:24
  • @Dici When i am trying to read the output stream as a buffered stream like:- BufferedReader reader=new BufferedReader(new InputStreamReader(os)); I get the error: error: no suitable constructor found for InputStreamReader(OutputStream) – user720694 Jul 28 '15 at 00:47
  • @Karan This is a compile error, and a pretty easy one. Just one the Javadoc as well as example of usage of `BufferedReader` – Dici Jul 28 '15 at 19:11
0

Additionally if you simply want to read console output you can do something like this

ProcessBuilder procBuilder = new ProcessBuilder("java", "-Dlog4j.configurationFile=src/test/resources/log4j2.xml", "-cp", "pathtojars com.mainclass", "\"parameters to mainclass\"", "\"inputfile\"");
String OUTPUT_FILE = "C:/temp/testFile.txt";
File outFile = new File(OUTPUT_FILE);
procBuilder.redirectOutput(outFile);
Process process = procBuilder.start();
outFile = new File(OUTPUT_FILE);
String content = FileUtils.readFileToString(outFile, "UTF-8");
System.out.println(content);

Or you can also write something like this ProcessBuilder: Forwarding stdout and stderr of started processes without blocking the main thread

As dici pointed out you first need to get the OutPutStream of the process and handle it in the way you want to.

Community
  • 1
  • 1
Acewin
  • 1,657
  • 4
  • 17
  • 36