简体   繁体   中英

Load external jar with system class loader in Jetty

I'm using Jetty (both 8 and 9) along with LZ4 compression and I'd like to use the native implementation of LZ4 which is a native binding to a C library. To accomplish this the LZ4 classes should be loaded with the system class loader.

So my question is, how to load an external jar with the system class loader in Jetty.

Placing the lib into $jetty.home/lib/ext didn't help as it was loaded with the extension class loader instead of the system one.

When Jetty starts, it instantiates a custom ClassLoader to conform to the various spec requirements (JSP, CDI, OSGi, Java9, etc.)

The System ClassLoader basically only exists to hold the JVM (and optionally the XBootClasspath and JVM extension) related jars.

The Jetty lib/ext is not related to the JVM extension mechanisms, don't get those 2 confused.

Perhaps your LZ4 compression jars need to be loaded via the various extension mechanisms of the JVM, before Jetty gets involved with the ClassLoaders.

Something like ...

$ java <load_LZ4_jars_via_ext_stuff> -jar start.jar
# example
$ java -Djava.ext.dirs=/full/path/to/lz4.jar -jar start.jar

Note: Java 9 changes everything again, so whatever you learn now will have to be forgotten and you'll need to use the new Java 9 techniques (see JEP 220 and its deprecation/replacement of JVM extension behaviors and near elimination of the concept of a System ClassLoader)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM