Skip to content

Instantly share code, notes, and snippets.

@mleinart
Created August 19, 2011 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mleinart/1157185 to your computer and use it in GitHub Desktop.
Save mleinart/1157185 to your computer and use it in GitHub Desktop.
Sanely load a class from a classloader in python
def loadClass(self,className, classLoader):
""" loadClass(className)
Load a class in classLoader and recursively load any dependencies. """
# From what I'm told there's no better way...
try:
classLoader.findClass(className)
except LinkageError:
pass
except ClassNotFoundException:
pass # i dunno.. sometimes findClass throws this but loadClass works
try:
return classLoader.loadClass(className)
except NoClassDefFoundError, e:
missingClass = e.detailMessage.replace('/', '.')
#If we really dont have a dependent class NoClassDefFound gets raised here
loadClass(missingClass, classLoader)
return loadClass(className, classLoader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment