How do I print the current classpath for a Jenkins plugin?

1k views Asked by At

I'm developing a Jenkins plugin but I'm having a few problems which I think might be due to a jar missing in the plugin's classpath even though its in the plugin's WEB-INF/lib directory.

Normally I would use the following code in Java to print the classpath but this doesn't seem to work with Jenkins.

ClassLoader cl = getClass().getClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
    System.out.println(url.getFile());
}

When I execute the plugin the above code causes a ClassCastException exception to be thrown with the message hudson.ClassicPluginStrategy$AntClassLoader2 cannot be cast to java.net.URLClassLoader

What is the best way to print the classpath being used by a Jenkins plugin?

1

There are 1 answers

0
Paul H On BEST ANSWER

Having read through the Javadoc for AntClassLoader it seems the solution was a lot easier than I was expecting and so I've written the following code which can be used print the classpath for a Jenkins plugin.

AntClassLoader cl = (AntClassLoader) getClass().getClassLoader();
String[] classpath = cl.getClasspath().split(":");
for (String classpathElement : classpath) {
    System.out.println(classpathElement);
}