How to retrieve constants value from compiled code in jar file?

Discussion in 'Java' started by, Jul 6, 2007.

  1. Guest

    Hi everyone,

    I've been looking for a way to solve this for a week now, without any
    much success... and I've finally decided to ask the Java gurus for a
    solution! :)
    Here's what I am basically trying to do:
    I have several jar files in which there are only compiled code
    In every class, there are 2 constants (declared as static final
    String) that I would like to retrieve (one is the version and the
    other the date of the last modification).
    My goal is to print a list of all the classes in the jar files with
    the values of these 2 constants for each class.

    The solution that I have right now to do this does not work properly:
    for now, I read all the elements of the classpath, check if these are
    jar files, and if so, I look into each one and load all the classes
    one by one and print the results.
    The problem with this is that it uses the method
    Class.forName(className) and as some classes are unfortunately present
    in many jar files (2 or 3 copies), once the classes have been loaded,
    then it won't be "reloaded".
    Without the possibility to "reload" these classes, I cannot see
    inconsistencies in the versions of the classes present in the jar

    I have read many articles, and I thought that I could then use a
    custom classloader and create a new instance of this classloader for
    each jar file.
    2 problems with this:
    - according to many posts in the different forums I have read, the jar
    files should not appear in the CLASSPATH (but this would be easier for
    me if I could use it...)
    - some classes will not be loaded if some classes (present in other
    jar files) are not loaded... and this makes things really really
    complicated to implement...

    So, I thought that I was maybe doing this the wrong way, and that
    there might be an easy way out of this...
    In fact, I do not need to load the classes... all I need to do, is
    take a sneak peek at the constants and print their values... and
    that's it!
    Somehow, I think that this is possible to retrieve the values of
    compile time constants (declared as static final String) as I can see
    that with Eclipse (when opening a jar file).
    So, my question is: how can I do that within my java application?
    Or maybe there is another easier solution to do what I need?

    Thanks in advance for your help!
    , Jul 6, 2007
    1. Advertising

  2. Roedy Green Guest

    On Fri, 06 Jul 2007 14:11:30 -0000,
    wrote, quoted or indirectly quoted someone who said :

    >I've been looking for a way to solve this for a week now, without any
    >much success... and I've finally decided to ask the Java gurus for a
    >solution! :)

    I have a number of similar student projects posted with suggestions on
    how to solve them. See

    That is pretty close to what you want to do.
    Roedy Green Canadian Mind Products
    The Java Glossary
    Roedy Green, Jul 6, 2007
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Oct 20, 2005
  3. cyberco
    Roedy Green
    Feb 14, 2006
  4. Arnold Peters
    Arnold Peters
    Jan 5, 2005
  5. Klaus Schneider
    Rolf Magnus
    Dec 2, 2004

Share This Page