Sunday, August 9, 2015

Check if a class is loaded and lib file location in JVM

Example below is to see if JVM using ojdbc6 version 11 or version 12.

final ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassPath clazzPath = ClassPath.from(loader);
Set<ClassInfo> classes = clazzPath.getTopLevelClasses();
for (final ClassPath.ClassInfo classInfo : classes) {
    if (classInfo.getName().startsWith("oracle")) {
         if (classInfo.getName().contains("oracle.jdbc.babelfish")) {
                 isVersion12 = true;
         }

    }
}
            

if(isVersion12) {
        LOGGER.info("Ojdbc version 12");

} else {
        LOGGER.info("Ojdbc version 11");

}

Class klass = OracleConnection.class;
URL location = klass.getResource('/' + klass.getName().replace('.',  '/') + ".class");
LOGGER.info(location.toString());

No comments:

Post a Comment