diff --git a/src/main/java/org/scijava/plugin/DefaultPluginFinder.java b/src/main/java/org/scijava/plugin/DefaultPluginFinder.java index 3c34ba6a5..20c58a30c 100644 --- a/src/main/java/org/scijava/plugin/DefaultPluginFinder.java +++ b/src/main/java/org/scijava/plugin/DefaultPluginFinder.java @@ -110,7 +110,24 @@ private PluginInfo createInfo( private ClassLoader getClassLoader() { if (customClassLoader != null) return customClassLoader; - return Thread.currentThread().getContextClassLoader(); + + /* + * If not even the current class can be found by the current + * Thread's context class loader, chances are that the plugins + * the caller tries to discover using this plugin finder cannot + * be found, either. Therefore let's use the current class' + * class loader in that case. This is not completely + * fool-proof, but better than nothing. + */ + final ClassLoader thisLoader = getClass().getClassLoader(); + final ClassLoader contextLoader = + Thread.currentThread().getContextClassLoader(); + for (ClassLoader loader = contextLoader; + loader != null; + loader = loader.getParent()) { + if (thisLoader == loader) return contextLoader; + } + return thisLoader; } }