Classloader issues - How to determine which library versions (jar-files) are loaded?

If you happen to be using JBoss, there is an MBean (the class loader repository iirc) where you can ask for all classloaders that have loaded a certain class.

If you happen to be using JBoss, there is an MBean (the class loader repository iirc) where you can ask for all classloaders that have loaded a certain class. If all else fails, there's always 'java -verbose:class' which will print the location of the jar for every class file that is being loaded.

Sounds like something I could use Tom! I'll dig into it tomorrow. Thanks in advance.... Johan – Johan Pelgrim Sep 29 '08 at 20:49.

In the current version of Java, library versioning is a rather wooly term that relies on the JAR being packaged correctly with a useful manifest. Even then, it's a lot of work for the running application to gather this information together in a useful way. The JVm runtime gives you no help whatsoever.

I think your best bet is to enforce this at build time, using dependency management tools like Ivy or Maven to fetch the correct versions of everything. Interestingly, Java 7 will likely include a proper module versioning framework for precisely this sort of thing. Not that that helps you right at this moment.

If you've got appropriate versions info in a jar manifest, there are methods to retrieve and test the version. No need to manually read the manifest. Java.lang.Package.

GetImplementationVersion() and getSpecificationVersion() and isCompatibleWith() sound like they'd do what you're looking for. You can get the Package with this.getClass().getPackage() among other ways. The javadoc for java.lang.

Package doesn't give the specific manifest attribute names for these attributes. A quick google search turned it up at java.sun.com/docs/books/tutorial/deploym....

I don't think there is a good way to check that. And I'm not sure you want to do that. What you need to do is get familiar with your app-server's class loading architecture, and understand how that works.

A simplified explanation of how it works is: an EJB or a web-app will first look for a class or a resource in libraries declared in its own module (ejb-jar or war). If the class is not found there, the class loader forwards the request to the its parent class loader which is either a declared dependency (usualy an ejb) or the the application class loader which is responsible to load libraries and resources declared in the ear package. If the class or the resource is still not found the request is forwarded to the app server which will look in its own classpath.

This being said, you should remember that a JEE module (web-app, ejb) will always load classes from a jar that is in the nearest scope. For example if you package log4j v1 in the war file, log4j v2 at ear level and you put log4j v3 in your app-server's class path, the module will use the jar in its own module. Take that away and it'll use the one at ear level.

Take that out and it will use the one in the app-server's classpath. Things get more tricky when you have complex dependencies between modules. Best is to put application global libraries at ear level.

For JBoss at least, it's not as simple as that. In some cases JBoss will load a class from outside your WAR file (e. G from server/default/lib) even though the class exists inside your WAR file.It depends on various factors such as whether scoped loading is enabled (the default value for which varies between JBoss versions) and whether delegation to the parent class repository is enabled.

– Andrew Swan Sep 9 '09 at 5:01.

If the manifest file of the jar files you're interested in has a version field then you can access it via: ZipFile zipJar=zipJar=new ZipFile("jarToMonitor. Jar"); ZipEntry zipEn=zipJar. GetEntry("META-INF/MANIFEST.

MF"); BufferedInputStream buf=new BufferedInputStream(zipJar. GetInputStream(zipEn)); Properties p=new Properties(); p. Load(buf); String build=p.

Get("version").toString(); zipJar.close(); To find the JARs loaded you can look in the classpath property: String classpath=System. GetProperty("java.class. Path").

Split(System. GetProperty("path. Separator"));` Then iterate around those (checking if they're directories then search for jar files).

If your application uses the above code to verify the loaded jars against the version is 'knows' it needs and shouts about it at runtime. You should know of problems at start-up. – Ron Tuffin Sep 26 '08 at 13:59 also, tha man says his application runs in an appserver.

Your code wouldn't work as libraries are not referenced through the classpath mechanism. – entzik Sep 26 '08 at 14:04.

There must be a better way than the way I do it, but I tend to do this in a very manual way. Every Jar must have it's version number in the file name (if it doesn't change it's name). Each application has it's own classpath.

There must be a reason to start using an updated Jar (new version). Don't just change because it is available, change because it gives you functionality that you need. Each release must include all Jars that are needed.

I keep a Version class that knows the list of Jars it needs (this is coded into the source file) and can be checked at runtime against the list of Jars in the classpath. As I said, it is manual, but it works.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions