3

I am reading about Jvm architecture but i could't understand class loader architecture in it.I understood that what a bootstrap classloader is , but could't understand working of user-defined classloader , how it works in jvm and why it is required?

Does programmer writes user-defined classloader? I have been working on java but have not used or could't see when it's used? Please try to explain in a easy to understand way so I can exactly understand that what it is.

Abhijeet Panwar
  • 1,837
  • 3
  • 26
  • 48
  • 1
    https://www.google.de/search?q=user+defined+classloader&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:de:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=rGGyU5D7KOqg8weh-YGoAQ – Seelenvirtuose Jul 01 '14 at 07:22

2 Answers2

5

What is user-defined classloader?

A user-defined classloader is a class (that is not provided by the standard Java libraries or 3rd-party libraries) that extends java.lang.Classloader or a subclass.

Does programmer writes user-defined classloader?

Yes.

how it works in jvm

Just like any other classloader. More specifically, to load a class, it loads the classfile into a byte[] and then calls the defineClass method implemented by the ClassLoader class. Other things may entail delegating to a parent classloader.

and why it is required?

It would be required if you need your classloader to behave differently to a normal classloader. For example, you might want to transform the bytecodes, or load them from a place that the standard implementations cannot cope with. Or you may want to implement resource loading differently to the normal pattern.

Please try to explain in a easy to understand way so I can exactly understand that what it is.

I suggest you read this IBM tutorial on classloaders: http://www.ibm.com/developerworks/java/tutorials/j-classloader/j-classloader.html It is designed to be easy to understand, and includes a worked example of a custom classloader. (It is a bit old, but that shouldn't matter.)

Alternatively, there are lots of alternatives ... as Google will tell you.

And if you just want to understand classloaders in general, read: What is a Java ClassLoader?

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

I'll try to keep it simple.

Java uses classloaders to load classes inside JVM. Java is able to load classes from binaries (*.class files) on filesystem or *.class files inside jar files.

ClassLoader is something that has a "loadClass" functionality - it gets a string name of the class (with package and everything) and returns Class - a class file that describes this object. Once loaded, the class can be used inside the JVM.

Example (pseudocode):

ClassLoader myCl = ...
Class<Foo> fooClass = myCl.loadClass("some.package.Foo")

Why would you even bother about classloaders? Well, usually you don't really create your own classloaders. Hoewever sometimes you need them.

Lets say, you're creating application server/web server. It's possible to load the application in runtime, applications should not interfere. In this case you might want to implement your own class loader and load each WAR with the help of its own classloader (this is essentially what actually happens in JBoss, tomcat and so forth).

Another example, Lets say you store your class definitions in some "weird" places - like database, you you get the class definitions via network. In this case you'll implement your classloader that fetches the class definition and then loads.

And the last note. Your own class loader shouldn't really "load" the class by itself, usually its only responsible to bring the byte stream and then call its parent's method to do actual loading (all class loaders extends java.lang.ClassLoader).

Of course its a very broad theme to be covered in this post, I just tried to give a general directions to start with. You should probably read some documentation about classloaders.

For example: this one should be good

Hope, this helps

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97