0

This is a follow up to java.net package - Override UDP transport. I am raising a new question here because I am trying a different approach to the problem. I have gone through simlar questions

java custom classloader: some classes are not loaded by my classloader

How to set my ClassLoader as the JVM's ClassLoader for loading ALL classes (jar classes included)?

but no luck. so here is the problem.

I am trying to intercept loading of all classes (including standard JDK classes) in my custom class loader. The idea is to load an extended class whenever there is a request for DatagramSocket class. I dont have a handle to when and how DatagramSocket would be instantiated in the application and hence I thought this is the best way to intercept loading of this particular class before I start the app from my main method. For this purpose I wrote TestClassLoader,

public class MyClassLoader extends ClassLoader
{

    public MyClassLoader(ClassLoader parent)
    {
        super(parent);
    }

    public Class loadClass(String name) throws ClassNotFoundException
    {
        System.out.println("loading " + name);
        if (name.equals("java.net.DatagramSocket")
                || name.equals("MyDatagramSocket"))
        {
            System.out.println("Loading DatagramSocket class.... " + name);
            return super.loadClass("MyDatagramSocket");
        }
        else
        {
            return super.loadClass(name);
        }
    }
}

and a test program,

public class TestClassLoader
{
    public static void main(String[] args) throws Exception {

        //See if this invokes myClassLoader
        DatagramSocket dSocket=new DatagramSocket();
        System.out.println("DatagramSocket.classloader = "+DatagramSocket.class.getClassLoader());
        //check the Class loader for the main class
        System.out.println("TestClassLoader.classloader = " + TestClassLoader.class.getClassLoader() );

    }
}

and invoked the test program using "-Djava.system.class.loader=MyClassLoader" option. The output I see is

loading java.lang.System 
loading java.nio.charset.Charset 
loading java.lang.String 
loading TestClassLoader 
DatagramSocket.classloader = null 
TestClassLoader.classloader = sun.misc.Launcher$AppClassLoader@130c19b

I dont see my test class loader being called when DatagramSocket is initialized. Am I doing this right ?

Community
  • 1
  • 1
user572964
  • 600
  • 5
  • 12
  • If you're trying to intercept class loading, have you considered using a custom javaagent instead? – Nicholas Daley-Okoye Feb 11 '15 at 18:22
  • No I have not tried custom java agent. Can I achieve the same result using javaagent, i.e., instantiate my extended class whenever DatagramSocket is created? I don't want to modify the standard DatagramSocket because this might cause some licensing issues. – user572964 Feb 11 '15 at 19:38
  • A java agent has to be applied at the time that the JVM is started. But you are able to modify classes as they are loaded. It is not easy, but you could replace the standard implementation with your own implementation. – Nicholas Daley-Okoye Feb 11 '15 at 22:13

0 Answers0