views:

44

answers:

2

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.

+1  A: 

Check this out: http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/net/URLClassLoader.html

angryundead
I already used the `URLClassLoader` with the `ClassLoader` before. However (As far as I know!) it's not possible to add HTTP Headers to the request using A `URLClassLoader`.
Gnarly
A: 

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.)

Bruno