Abhishek Lodha

Greenhorn
+ Follow
since Sep 23, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Abhishek Lodha

Hi


I have a page where after clicking on the button the page is redirected based on the input selected. As soon as we click on the button "the page is being redirect, please wait" comes. when i dont get the redirected page when I printing page.asXml(); the page stays the same with the redirecting message.

Please help

Thanks
12 years ago
Hi

1. ClassNotFoundExcpetion occurs during loading phase while NoClassDefFoundError occurs in Linking phase

Is the above statement correct?

2. While creating a custom class loader How can I simulate a situation where class loading fails at resolveClass(clas) method?

3. In resolveClass(clas) we link the class. correct?
12 years ago

Ernest Friedman-Hill wrote:

Abhishek Lodha wrote:
let MyObject class not be in the classpath and let its superclass be in the classpath. This way MyObject will be loaded by custom class loader and superclass(MySuperObjectClass) will be loaded by system class loader. This way will I be able to cast?



That is correct, if your classloader properly delegates to the system class loader.



Thanks a lot. I got the point now.
12 years ago


Is there any particular reason you wrote your own classloader, rather than simply using URLClassLoader?



Just wanted to learn how class loader works that's why I dont want to use URLClass Loader.

My requirement is
Load MyObject class using custom class loader and cast it to its superclass(MySuperObjectClass).

Here is what I understood from the above replies.

let MyObject class not be in the classpath and let its superclass be in the classpath. This way MyObject will be loaded by custom class loader and superclass(MySuperObjectClass) will be loaded by system class loader. This way will I be able to cast?
12 years ago

Jeff Verdegan wrote:

Abhishek Lodha wrote:
SOP(MyObject.class.getClassLoader()); //This return system class loader Why? when this class has already been loaded by my custom class loader.



MyObject is on your classpath, so the your classloader delegates up to the System classloader to load it. Any class to be loaded by your classloader must NOT be in your classpath.



MyObject is in classpath but I am overriding the default way classloaders are created. I am not delegating the request to load the class system class loader but I am loading it from my classloader.
12 years ago
I need some more help

1.
We say that a class is loaded only once by system classloader.

Create any random Object and then get the system class loader from this object.

AnyClass obj = new AnyClass; //This class will be loaded by system class loader.

ClassLoader cl = obj.getClass().getClassLoader(); //getting the system class loader.

cl.loadClass("AnyClass"); // changing the AnyClass.class and reloading it.

This way we can reload a class using system class loader.

2.
We also say that we that custom classes are used to load a class whose contents are dynamically generated.
This thing we can do from system class as well. loading the contents to class file and loading that class file using above method.

cl.loadClass("dynamicClass");

Thanks
12 years ago
Thanks for your answers I will try not putting the class in the classpath.
12 years ago
1.
I have created a custom class loder.

ClassLoaderDemo ccl = new ClassLoaderDemo(); //assume it is working fine
Class clas = ccl.loadClass( "com.MyObject" );

com.MyObjectSuperClass o1 = (com.MyObjectSuperClass)clas.newInstance(); //MyObjectSuperClass is super class of MyObject

Now I want to cast the loaded object to MyObjectSuperClass which is super class if MyObject. But I am getting the following exception

Exception in thread "main" java.lang.ClassCastException: com.MyObject cannot be cast to com.MyObjectSuperClass

2.
Object o = ccl.loadClass( "com.MyObject" ).newInstance();
SOP(o.getClass().getClassLoader()); //This statement returns my custom class loader as excpected.

but

SOP(MyObject.class.getClassLoader()); //This return system class loader Why? when this class has already been loaded by my custom class loader.

Please help.
12 years ago