more .o vs .class and classloaders

steve steve@netfuel.com
Thu Jun 12 20:06:00 GMT 2003


Is is possible to do something like this using pure machine code, not 
byte code.  If I compile app.java and cl.java and  leave the two Hello's 
as class files it works.  How can   I make them .o or .so without 
changing the java code.  If I have to change the java code what changes 
need to be made?

app.java
class app
{
     public static void  main(String[] args) {
         try {
             java.io.File file = new 
java.io.File("/opt/NetFuel.save/gcj/gcj-jvm/cl/v1/");
             ClassLoader cl1 = new cl(file.toURL());
             file = new 
java.io.File("/opt/NetFuel.save/gcj/gcj-jvm/cl/v2/");
             ClassLoader cl2 = new cl(file.toURL());

             Class Klass1 = cl1.loadClass("Hello");
             Class Klass2 = cl2.loadClass("Hello");

             String 
s1=(String)Klass1.getMethod("HelloString",null).invoke(null,null);
             String 
s2=(String)Klass2.getMethod("HelloString",null).invoke(null,null);

             System.out.println("String from class 1 " + s1 + "\n");
             System.out.println("String from class 2 " + s2 + "\n");
         } catch (Exception e) {
             System.out.println("ERROR \n\t" +e);
         }
     }
}


cl.java
class cl  extends java.net.URLClassLoader
{
     cl(URL path){
         super (new URL[0]);
         try {
             addURL(path);
         } catch (Exception e) {
             System.out.println("Error in cl\n\t" +e );
         }
     }
}


v1/Hello.java
public class Hello
{
public static String HelloString() { return "V1 Hello";};
}

v2/Hello.java
public class Hello
{
public static String HelloString() { return "V2 Hello";};
}


-- 
Steve Pribyl
Steve AT NetFuel dot com
Computer Infrastructure Practitioner




More information about the Java mailing list