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.

3
  • 2
    Do you know what functions are called internally? Just wondering if you can add it to your project and use it as a library. Commented Nov 20, 2013 at 9:07
  • why not add classes from that 3rd party project to your project and treat them like normal library? Commented Nov 20, 2013 at 9:26
  • The problem is I don't know what functions are called. Commented Nov 20, 2013 at 9:36

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is I don't know what functions are called. Also, it is possible to extract the jar output into a String?
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
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.