Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get, at runtime, the version of Guava in use?

Tags:

java

guava

I did a quick grep of the Guava source and documentation, and neither seem to have any mention of versions. I was wondering if there was a way Guava's version information can be obtained at runtime.

This version information doesn't have to be accessible via any getter, if no such thing actually exists; if it's stashed in a field somewhere that doesn't get GC'd while Guava is loaded, that would be sufficient.

Is this version information available anywhere at runtime?


I have a very specific use for this. A big part of my work is analysing Java heap dumps to identify and fix places in the code that cause exorbitant memory usage. For this task, I use fasthat, a heavily-modified version of jhat with special features useful to my work.

One of those features is to display the contents of containers. I've already implemented this for the likes of ArrayList, HashMap, ConcurrentHashMap, etc. (I implement type printers on demand, based on what I encounter in our heap dumps.) Currently, I'm implementing a type printer for Guava's CustomConcurrentHashMap.

Since the layout of structures can change between versions, my code tweaks its unpacking behaviour based on what version is in use. For example, at work, we used to use JRuby 1.2, but recently switched to JRuby 1.6, so I have type printers for both of those versions, and it selects the version based on the version information it finds in the heap dump.

So, that's the point of the second paragraph of the question: if the version information is anywhere in the heap dump, that's all I need.

And before anyone asks: heap dump data is not "live", so you cannot simply call toString or the like. You really have to walk the data structures to extract the bits out, and you really do have to use implementation details to the nth degree. ;-)

like image 763
Chris Jester-Young Avatar asked Oct 08 '11 02:10

Chris Jester-Young


People also ask

How do I know my guava version?

You can get a version string from files under META-INF/ in the guava. jar. At runtime you'll need to use appropriate ClassLoader instance in order to load the META-INF/ files you want.

What is guava JRE version?

Guava provides two different "flavors": one for use on a (Java 8+) JRE and one for use on Android or by any library that wants to be compatible with Android. These flavors are specified in the Maven version field as either 31.1-jre or 31.1-android . For more about depending on Guava, see using Guava in your build.

What is Google Guava used for?

Guava is an open-source “Collection Library” library for Java, developed by Google. It provides utilities for working with Java collections. As you dive deep into Guava you'll notice how it reduces coding errors, facilitates standard coding practices and boost productivity by making code concise and easy to read.

What is guava selenium?

Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, hashing, caching, primitives, strings, and more!


2 Answers

You can retrieve the version from Guava's JAR manifest.

public static String getGuavaVersion() {
    try {
        File file = new File(Charsets.class.getProtectionDomain().getCodeSource().getLocation().toURI());
        try (JarFile jar = new JarFile(file)) {
            return jar.getManifest().getMainAttributes().getValue("Bundle-Version");
        }
    } catch (Exception ex) {
        throw new RuntimeException("Unable to get the version of Guava", ex);
    }
}

Unfortunately, this works only for Guava 11+. Guava 10 and older were not OSGI bundles yet.

Another option is to retrieve the version from pom.properties. This works for old versions of Guava too: https://gist.github.com/seanizer/8de050427f3f199cf8f085b2a3a2473e

like image 93
ZhekaKozlov Avatar answered Sep 24 '22 16:09

ZhekaKozlov


If you want to get the version of a maven built class, you can start with the class, find the jar it comes from and read the meta information which maven adds (such as the version)

A simpler way to do this, if the version is in the path, is to look at your classpath, find guava and the version you are using from the file name.

For a heap dump, the class path is in a System property.

like image 40
Peter Lawrey Avatar answered Sep 21 '22 16:09

Peter Lawrey