Skip to content

Instantly share code, notes, and snippets.

@seansummers seansummers/
Last active Apr 11, 2019

What would you like to do?
Jython classPathHacker to load JDBC
def loadJar(jarFile):
'''load a jar at runtime using the system Classloader (needed for JDBC)
adapted from
Author: Steve (SG) Langer Jan 2007 translated the above Java to Jython
Author: simplified and updated for jython-2.5.3b3+
>>> loadJar('jtds-1.3.1.jar')
>>> from java import lang, sql
>>> lang.Class.forName('net.sourceforge.jtds.jdbc.Driver')
<type 'net.sourceforge.jtds.jdbc.Driver'>
>>> sql.DriverManager.getDriver('jdbc:jtds://server')
jTDS 1.3.1
from java import io, net, lang
u = io.File(jarFile).toURL() if type(jarFile) <> net.URL else jarFile
m = net.URLClassLoader.getDeclaredMethod('addURL', [net.URL])
m.accessible = 1
m.invoke(lang.ClassLoader.getSystemClassLoader(), [u])
if __name__ == '__main__':
import doctest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.