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?

9
  • Ok, and what is the question Commented Jul 27, 2015 at 20:13
  • @Dici Question updated. Commented Jul 27, 2015 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. Commented Jul 27, 2015 at 20:18
  • 1
    possible duplicate of Execute another jar in a java program Commented Jul 27, 2015 at 20:21
  • You can get more idea from this stackoverflow.com/questions/14165517/… Commented Jul 27, 2015 at 21:23

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

6 Comments

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?
Not really, as long as you set the working directory of the ProcessBuilder to the right place
Ass dici mentioned what you are trying to read is output (console output) of a process execution
@Acewin You should post your own answer instead of editing mine
@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)
|
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.

Comments

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.