Results 1 to 5 of 5
  1. #1
    MichelPrevost is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default JAR dynamic loading and reflection

    I would like to programmatically load a JAR file and instantiate classes within it, possibly using the reflect API. Could anybody provide pointers to examples/tutorialss on how this can be done?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: JAR dynamic loading and reflection

    Create a ClassLoader (for instance URLClassLoader or ClassLoader.getSystemClassLoader) then load the Class object using Class.forName("") syntax, specifying the package and class as the parameter. From there, you can instantiate from the Class object using reflection.

  3. #3
    MichelPrevost is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default Re: JAR dynamic loading and reflection

    Thanks for the quick answer. The problem is that I am faced with 2 challenges:

    1- The JAR files will be in the same location, but I don't know their name until run-time. If it is not possible to do it, I may have a work-around, but I would like to try this approach first.

    2- The class names are unknown either, they will simply sport an annotation. So I need to find those classes inside the JARs. All the classes having the annotation.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: JAR dynamic loading and reflection

    A Jar file is essentially a Zip file. You can iterate through the class files of a jar using the JarFile class, similar to a Zip file. So the workflow would be something like: get/create a URLClassLoader, add the jar to the loader using addURL, iterate through the jar entries, for each class entry in the jar, load the class using the URLClassLoader loadClass method. From there you have a Class object and can use reflection to do what you want.

  5. #5
    MichelPrevost is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default Re: JAR dynamic loading and reflection

    OK, thanks, I will work it out starting from there. thank you!

Similar Threads

  1. Dynamic image loading in jsp
    By ramakrishna k m in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 10-10-2011, 07:08 AM
  2. Dynamic class loading problems
    By mani1723 in forum Advanced Java
    Replies: 0
    Last Post: 10-16-2010, 10:29 PM
  3. Dynamic Class Loading
    By dieter1990 in forum Advanced Java
    Replies: 2
    Last Post: 08-27-2010, 06:09 PM
  4. Replies: 8
    Last Post: 04-14-2010, 06:49 AM
  5. Dynamic Class Loading from external Jar
    By AndreB in forum Advanced Java
    Replies: 3
    Last Post: 02-08-2010, 02:55 PM

Posting Permissions

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