Quantcast
Channel: Heap Dump » Structured Content
Viewing all articles
Browse latest Browse all 10

Dynamically extending the Class Path

$
0
0

I need today to be able to extend the class path. The reason for this, is that although you pass classpath arguments to JavaDoc, when the Doclet is executed, the classpath isn’t honoured. The good news is that the classpath is passed to the Doclet as part of the parameters.

Anyway, this little utility method helps..

public static void extendClassPath(String path)
    throws DocfactoException {
        File filePath = new File(path);

        if (!filePath.isDirectory()) {
            throw new DocfactoException("Path doesn't exist or not a directory");
        }

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        if (cl instanceof URLClassLoader) {
            URLClassLoader ul = (URLClassLoader)cl;

            Class<?>[] paraTypes = new Class[1];
            paraTypes[0] = URL.class;

            try {
                Method method =
                    URLClassLoader.class.getDeclaredMethod("addURL",paraTypes);

                method.setAccessible(true);
                Object[] args = new Object[1];

                args[0] = filePath.toURI().toURL();
                method.invoke(ul,args);
            }
            catch (Exception ex) {
                throw new DocfactoException("Unable to extend class path ",ex);
            }
        }
        else {
            throw new DocfactoException(
                "System Class loader is not a URL class loader");
        }
    }

The post Dynamically extending the Class Path appeared first on Heap Dump.


Viewing all articles
Browse latest Browse all 10

Trending Articles