Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
lilisbbcsweet@googlemail.com - 06 Jul 2007 15:11 GMT
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
(.class).
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
files.

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!
Roedy Green - 06 Jul 2007 15:25 GMT
>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
http://mindprod.com/projects/interfacefinder.html

That is pretty close to what you want to do.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.