1

I have a .jar file which I can run from the Terminal on OS X, which takes input from the Terminal and yields output in the Terminal as well. Is it possible to incorporate this .jar file into an Eclipse Java project, where I can give the input within the Java project that goes to the .jar file and also receive the output as a String or something I can manipulate?

Thanks a lot.

Sam
  • 45
  • 5

2 Answers2

3

The short answer is yes.

The "dumb" approach would be to use a ProcessBuilder to interact with the jar via the command-line programatically.

A better approach would be if the jar exposes an API of some kind. Then you just need to add the jar to the classpath and use the functions like you would any other function.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Kevin is right on for both scenarios. To build on his first suggestion, I recommend using the Apache Commons IO library if you intend on doing any type of advanced IO maniupulation (or aren't just looking for a quick and dirty method). http://commons.apache.org/proper/commons-io/ – kwikness Mar 16 '15 at 18:00
1

I guess this question covers your problem pretty well. I will just copy paste the solution from the question linked above.

Just start the .jar in a new Process and get the in- and output streams:

Process proc = Runtime.getRuntime().exec("java -jar A.jar");
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
Community
  • 1
  • 1
7Z0nE
  • 21
  • 3