I created another thread for this problem, please refer to the original thread "caching downloaded classes".

 

Here is the complete code:

 

import java.net.*;

import java.io.*;

import java.lang.reflect.*;

 

class Connect

{

 

public static void main(String...a)

{

   ClassLoader loader = new NetworkClassLoader();

   try{

       Class<?> c;

       Method m;

       c = loader.loadClass("tryme1");

       m = c.getMethod("p", new Class[]{});

       m.invoke(null,new Object[]{});

      }catch(Throwable t){System.out.println(">>>>>>>> "+t);}

}

 

/*******************************************************************************************************/

 

public static byte[] downloadByteCodesFromURL( DataInputStream in) {

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    while (true) {

           try {

                outStream.write(in.readByte());

           } 

           catch (IOException e) { 

                  break; 

           }

    }

    return outStream.toByteArray();

}

 

/*******************************************************************************************************/

/*******************************************************************************************************/

 

static class NetworkClassLoader extends ClassLoader

{

public Class findClass(String name)

{

Class c;

 

try{

   URL u = new URL("http://localhost:80/"+name+".class");

  InputStream input = u.openStream();

  DataInputStream data = new DataInputStream(input);

  byte classBytes[] = downloadByteCodesFromURL(data);

  c = defineClass(name, classBytes, 0, classBytes.length);

}catch(Throwable t){System.out.println(t);c = null;}

return c;

 

}

}

}

 

tryme1.p() calls tryme2.p() which calls tryme3.p(). tryme3.p() just displays a JFrame.

 

Sorry, I have a problem with my browser, so I cannot apply code highlighting.

 

My question is: will this code cause the JVM to cache the loaded classes?

 

Thank you

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners