How to retrieve constants value from compiled code in jar file?
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
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
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!
Re: How to retrieve constants value from compiled code in jar file?
On Fri, 06 Jul 2007 14:11:30 -0000, firstname.lastname@example.org
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
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
|All times are GMT. The time now is 02:18 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.