zaterdag 21 december 2013

Dynamically load jar file in systemclassloader

This example is showing how to download a jar file into the jvm and use the classes in that jar file. First we create a jar file with a Hello.class in it. The Hello.class does some logging when its get called. After creating the jar file we set up a HTTP server which alows us to download the jar file from a url. The next step is creating a Jython application which after is start will download the jar, put in into the system class loader and run the Hello.class. 1) Creating the Hello.class. This is a very basic class, which is printing if a method is called.
package nl.sysgarden;

public class Hello {
 public Hello() {
 public static void main (String[] args){
 public void p1(){
2) Creating a jar file: After compile 'javac nl/sysgarden/' we put the class in the jar file httpjartest.jar
user$ jar cvf httpjartest.jar nl/sysgarden/Hello.*
added manifest
adding: nl/sysgarden/Hello.class(in = 540) (out= 343)(deflated 36%)
adding: nl/sysgarden/ = 272) (out= 146)(deflated 46%)
Testing the jar file bij calling the Hello class main function
user$ java -cp httpjartest.jar nl.sysgarden.Hello
3) Putting up the http deamon. Go to the directory where the jarfile is and activate the python webserver.
python -m SimpleHTTPServer 8080
Now we can download the jar file bij it's url http://localhost:8080/httpjartest.jar Next and final is creating the Jython app that starts and dynamicly adds the httpjartest.jar file to the systemclassloader.
$ cat 
import java.lang.System

def printObject(obj):
 print 'obj:' + str(obj) 
 print 'cls:' + str(obj.getClass().getName())
def addJarToClassLoader(u):
        sysloader = java.lang.ClassLoader.getSystemClassLoader()
        sysclass = sysloader.getClass()
        #print str(sysclass.getDeclaredMethods())
        method = sysclass.getDeclaredMethod("addAppURL", [])
        print 'method= ' + str(method)
        jar_a =
        b = method.invoke(sysloader, [jar_a])
        return sysloader

print 'step 1 download jar file and add it to the systemclass loader' 
sysloader = addJarToClassLoader('http://localhost:8080/httpjartest.jar' )
import nl.sysgarden.Hello
print 'step 2 create Hello object and print output' 
Running the file
bash-3.2$ java -jar jython-standalone-2.5.3.jar 
step 1 download jar file and add it to the systemclass loader
method= void sun.misc.Launcher$AppClassLoader.addAppURL(
step 2 create Hello object and print output