0

I have a certain Java program which convert an Image and produces the traits of those image (Mean red, Stdev Red, etc) I run the program this way java –jar TheProgram.jar Picture.jpg Which produces this output FileName [feature1,feature2,..]

However I need to process those output into more proper format, I will use Java to process the outputy. The problem is I dont't know how to call the TheProgram.jar inside the Java source code.

IllSc
  • 1,419
  • 3
  • 17
  • 24

2 Answers2

0

One way would be to use the

Class jarClass = Class.forName("com.className");
ClassName c = jarClass.newInstance();
Method mainMethod  = Class.getMethod("main", String[]);
Method.invoke(c, "Picture.jpg");

replace the ClassName with the name of the Class in the jar in which the main method resides.

Linus
  • 880
  • 13
  • 25
  • The problem is I don't know what functions are called. Also, it is possible to extract the jar output into a String? – IllSc Nov 20 '13 at 09:42
  • you can decompile the classes to find the exact class containing the main function. and System.setOut(PrintStream out) may be used to set appropriate out mechanism – Linus Nov 20 '13 at 09:53
0

First, the way to run the program is as follows:

  1. Use "jar -xv TheProgram.jar" to unpack the JAR file
  2. Open the the MANIFEST.mf file in an editor and look for the "Main-Class" entry. That gives you a class name; e.g. "com.example.TheProgramMain"
  3. Now write a class like this:

      import com.example.TheProgramMain;
    
      public class MyClass {
          ...
          public void runIt() {
              String[] args = ...;
              TheProgramMain.main(args);
          }
      }
    

(You could automate that, i.e. using reflection, but there's not a great deal of point.)

That's the easy bit. The hard bit is going to be intercepting the output from the existing program so that you can post-process it. How you do that will depend on how the existing program works ...

Note that if you understood the internal design of the program, you could potentially figure out other ways to "drive" it; e.g. by instantiating classes and calling public instance methods to do the processing without writing the output.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216