Have you ever wondered where your class file currently loaded by your class loader?

There are instances where you bundle same class file in different jars and distribute it. You may tend to have them in your application’s class path so that it can be loaded by the class loader and referenced at run time. Do you want to know which class file you’re referring to at run time? There are a couple of ways to achieve this:

Using VM parameter: -XX:+TraceClassLoading
Using the following code snippet

        public static URL getClassLocation (final Class cls)
        {
            if (cls == null) throw new IllegalArgumentException ("null input: cls");
          
            URL result = null;
            final String clsAsResource = cls.getName ().replace ('.', '/').concat (".class");
          
            final ProtectionDomain pd = cls.getProtectionDomain ();
            // java.lang.Class contract does not specify if 'pd' can ever be null;
            // it is not the case for Sun's implementations, but guard against null
            // just in case:
            if (pd != null)
            {
                final CodeSource cs = pd.getCodeSource ();
                // 'cs' can be null depending on the classloader behavior:
                if (cs != null) result = cs.getLocation ();
              
                if (result != null)
                {
                    // Convert a code source location into a full class file location
                    // for some common cases:
                    if ("file".equals (result.getProtocol ()))
                    {
                        try
                        {
                            if (result.toExternalForm ().endsWith (".jar") ||
                                result.toExternalForm ().endsWith (".zip"))
                                result = new URL ("jar:".concat (result.toExternalForm ())
                                    .concat("!/").concat (clsAsResource));
                            else if (new File (result.getFile ()).isDirectory ())
                                result = new URL (result, clsAsResource);
                        }
                        catch (MalformedURLException ignore) {}
                    }
                }
            }
          
            if (result == null)
            {
                // Try to find 'cls' definition as a resource; this is not
                // documented to be legal, but Sun's implementations seem to allow this:
                final ClassLoader clsLoader = cls.getClassLoader ();
              
                result = clsLoader != null ?
                    clsLoader.getResource (clsAsResource) :
                    ClassLoader.getSystemResource (clsAsResource);
            }
          
            return result;
        }

1 comment:

  1. Magesh:

    This is an useful code. As an Admin i am not sure which version of a class is being used due to many Jars included in the EAR file.

    Meenakshi Sundaram

    ReplyDelete

Followers