Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34672 > unrolled thread

How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes?

Started bykant kodali <kanth909@gmail.com>
First post2016-06-20 00:44 -0700
Last post2016-06-20 11:01 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes? kant kodali <kanth909@gmail.com> - 2016-06-20 00:44 -0700
    Re: How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes? Steven Simpson <ss@my.domain.invalid> - 2016-06-20 11:01 +0100

#34672 — How to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes?

Fromkant kodali <kanth909@gmail.com>
Date2016-06-20 00:44 -0700
SubjectHow to write a URLClassLoader in Java such that it can execute a block of code prior to loading certain classes?
Message-ID<d51782b1-c257-4353-9295-a23bf31c0740@googlegroups.com>
    public class MyClassLoader extends URLClassLoader {
    
        public MyClassLoader() throws Exception{
             super(new URL[]{new URL("file:///home/ubuntu/java/hello.jar")});
        }
    
        @Override
        public Class loadClass(String name) throws ClassNotFoundException {
            if (name.equals("hello.example.org.Foo") || name.equals("hello.example.org.Bar")) 
            {
                System.out.println("Foo or Bar is loaded");
            }
            return super.loadClass(name);
    
        }

}

I just want to execute a block of code say a print statement prior to loading certain classes and the above code doesn't quite work and I am not sure where the error is?

[toc] | [next] | [standalone]


#34675

FromSteven Simpson <ss@my.domain.invalid>
Date2016-06-20 11:01 +0100
Message-ID<pgrk3d-it3.ln1@s.simpson148.btinternet.com>
In reply to#34672
On 20/06/16 08:44, kant kodali wrote:
>          @Override
>          public Class loadClass(String name) throws ClassNotFoundException {

> I just want to execute a block of code say a print statement prior to loading certain classes and the above code doesn't quite work and I am not sure where the error is?

<http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#loadClass-java.lang.String->

loadClass(String) just calls loadClass(String, boolean).  It's possible 
that the latter is being called directly, by-passing your override.

-- 
ss at comp dot lancs dot ac dot uk

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web