Results 1 to 4 of 4
  1. #1
    robby14 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default calling a jar file from within java application

    Hi,
    I am trying to use daisydiff and it provides a jar file which I easily manage to run from cmd passing it parameters (2 file names). I need to call this jar file - passing it the parameters as well, from within my java application. Is it possible please? and if yes could you guide me how I should do it?

    thanks

    Roberta

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    If the jar file that contains his utility with the main method is in the class path. from your code you should be able to use the reflection API to invoke the method. Get a handle on the class, and then the method, and invoke it with the string parameters that you would have used on the command line.
    Java Code:
    String mainClassName="my.package.class.Name"; // set this.
    String[] args = new String[] {"param1", "param2" }; // and set the params here
    
    //
        Class<?> mainClass = null;
        try {
          mainClass = Class.forName(mainClassName);
        }
        catch (Exception ex) {
          throw new IllegalArgumentException("class not found in your jar file " + mainClassName);
        }
        
        // ok, class loaded, now find the main method
        Method mainMethod = null;
        try {
          mainMethod = mainClass.getMethod("main", String[].class);
        }
        catch (Exception ex) {
          throw new IllegalArgumentException("class to launch (" + mainClassName + ") does not have a public static void main(String[]) method");
        }
        
        // and invoke main()
        mainMethod.invoke(null, (Object) args);

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by travishein View Post
    If the jar file that contains his utility with the main method is in the class path. from your code you should be able to use the reflection API to invoke the method. Get a handle on the class, and then the method, and invoke it with the string parameters that you would have used on the command line.
    You don't need reflection for that, you can call the main( ... ) method of that (known) class directly. If you don't know the class beforehand you indeed need reflection trickery dickery.

    kind regards,

    Jos

  4. #4
    robby14 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default

    thanks. very useful information. I think I ll have to try the reflection thing.
    thanks very much again

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2010, 05:46 AM
  2. Replies: 0
    Last Post: 08-10-2009, 02:25 PM
  3. excecuting a jar file by calling a java class
    By Lavanya.vitria in forum Advanced Java
    Replies: 1
    Last Post: 12-13-2008, 01:11 PM
  4. Batch file to Run Java application
    By nitishlnt in forum Eclipse
    Replies: 1
    Last Post: 10-24-2008, 08:46 PM
  5. calling a java application from another
    By kratoras in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 03:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •