简体   繁体   中英

Loading a JAR file from a URL

How can I load a JAR file from a URL in Java while making the request for the file seem legit by adding necessary HTTP Headers into the request.

The only way I know how to maintain the loaded JAR file at the moment is to use ClassLoader .

I think I nailed this question pretty good (Or so it seems that way in my head!) but if anyone needs me to provide more information before they can help me, be sure to ask me.

You may be able to pass specific HTTP headers by passing a custom URLStreamHandlerFactory in a URLClassLoader constructor .

Note that this is slightly different from creating the URLClassLoader using newInstance as it doesn't use checkPackageAccess from the SecurityManager (so you might have to do that yourself).

Alternatively, perhaps jcloader could be of interest (but I haven't tried it): it might be easier to make a more traditional HTTP request (without using a URLStreamHandlerFactory ), potentially with the HTTP headers you want, and then load the result using one of its classloaders (as an InputStream or something similar).

(There are of course other security implications for loading a jar file from a remote source, as you may need to verify you trust the integrity of this file.)

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