• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class Loaders  RSS feed

 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are ClassLoaders?? I mean, how exactly do they work??
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, they read the bytecode produced by the compiler and create an instance of Class in memory.

There are interesting variations in where they read the bytecode, such as from disk or over a network, but they search through the classpath in order to find a given class.

There is also an interesting hierarchy. When the JVM or a server product or your own code creates a new classloader, the new loader always has a parent loader. When it's time to load a class, the loader asks its parent to try first. Only if the parent can't do the job does the new loader do anything. That can happen because the new loader knows a different classpath than the parent.

Have you run into specific examples or situations that raised more detailed questions?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the API documentation for class ClassLoader, it explains what a class loader is.

For simple "What is ...?" questions, please try Wikipedia or Google first.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.
I started this topic here to have an interactive discussion with ranchers, I may get the information about almost anything from Wiki or Google, but it is its discussion that matters....
Thanks anyway.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
In short, they read the bytecode produced by the compiler and create an instance of Class in memory.


While that's a concise explanation of what they do, I think one points needs to be elaborated upon: where the classloader gets those bytes that make up a class. For a desktop application, that would be the classpath. For a web app, that would be the WEB-INF/lib and WEB-INF/classses directories. For an applet it would be a directory on the web server where the applet is hosted, and the access happens through HTTP, not file I/O like with the other above-mentioned classloaders.

In addition there are specialty classloaders that do not read class files, but create bytecode themselves, e.g. the CompilingClassLoader or this one, both of which read Java source code. Or this one, which constructs classes in memory through an API, where nothing ever gets stored on disk.

Another application would be to use them in conjunction with a SecurityManager in order to restrict what certain code is allowed to do. This is sometimes used if an application can be extended by user-supplied plugins (about which I've written here, if I may strut my own stuff).
[ April 12, 2007: Message edited by: Ulf Dittmer ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I left a lot out the first time around.

I've been intrigued by a couple tools that do AOP style byte code modification in the class loading process. There are a lot of ways to get tricky with loaders fer sure.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I've been intrigued by a couple tools that do AOP style byte code modification in the class loading process.


That does sound interesting. Do you happen to remember the names of those projects, or have some URLs?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We used Wily Introscope that does this to instrument classes for monitoring. It inserts some bytecode to register every method entry and exit, capture exceptions and SQL and all kinds of cool things. I heard a podcast interview with designers of one of the IDEs (not Eclipse is all I remember) that did the same kind of thing on entry-exit. I got the impression it's becomming common practice in the performance monitoring field.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!