开发者

If multiple versions of jars are there in application how to find Jvm is using which one

开发者 https://www.devze.com 2023-01-02 02:00 出处:网络
If multiple versions of jars are there in applica开发者_如何学Ction how to find JVM is using which one?The JVM looks for the classes in the order the jars and directories are on the classpath.

If multiple versions of jars are there in applica开发者_如何学Ction how to find JVM is using which one?


The JVM looks for the classes in the order the jars and directories are on the classpath.

Be careful with application servers etc, as they tend to replace classloaders, and in that case it might be different.


If you can, add the -verbose flag to the java startup commandline, this will print to stdout which classes are loaded and from which source. The output looks like:

[Loaded javax.xml.transform.stream.StreamResult from C:\Program Files\Java\jre6\lib\rt.jar]

If you redirect the output into a file you can parse or search the information you need more easily.


It's not predictable.

Avoid having multiple versions of one jar on the classpath. If you are forced to, by an application server, it usually offers an configuration to prefer bundled vs app-specific libraries.

If you need something more complex, take a look at OSGi - it allows you to use multiple versions in the same runtime.


A bit high level and I'm not sure you're using Spring, but if you are - it has a very useful utility class which serves me well in such cases:

PathMatchingResourcePatternResolver looks for resources (files) in various ways - relative to classpath / web app root / file system / etc. If you ask it to look for the file in which the class you're looking for is found, it will find it in ALL classpath entries. I use it like this (usually in some debugger's eval/watch):

Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:your/package/YourClassName.class");

** This will not tell you which of the classes is used by the code, but it might assist you in pinpointing your issues in case you're not sure where the classes are coming from.

0

精彩评论

暂无评论...
验证码 换一张
取 消