aspose file tools
The moose likes Java in General and the fly likes Advantages of User Defined ClassLoader? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Advantages of User Defined ClassLoader?" Watch "Advantages of User Defined ClassLoader?" New topic

Advantages of User Defined ClassLoader?

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Hi Guys,

From your understanding of an UserDefined Class Loader, Can you tell me the advantages we can have using it?


Be Humble... Be Nice.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40518
Hardly a beginner's question.

Moving . . .
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
It can load classes that are not in the classpath - over the network, out of a database, from Java source code, out of a plugin directory, or generate them on the fly. Granted, most applications don't need that kind of functionality, but it can be very handy. This -ahem- is a nice opportunity to plug an article I wrote for the JavaRanch Journal a while ago; in it, a custom classloader and a custom security manager are defined (for good cause!)
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Thanks Ulf.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 507

I think i am trying to write benefit of User-Define Class loaders as follows:-
Because of user-defined class loaders, you don't have to know at compile-time all the classes that may ultimately take part in a running Java application. User-defined class loaders enable you to dynamically extend a Java application at run-time. As it runs, your application can determine what extra classes it needs and load them through one or more user-defined class loaders. Because you write the class loader in Java, you can load classes in any manner expressible in Java code. You can download them across a network, get them out of some kind of database, or even calculate them on the fly.
For each class it loads, the Java virtual machine keeps track of which class loader--whether bootstrap or user-defined--loaded the class. When a loaded class first refers to another class, the virtual machine requests the referenced class from the same class loader that originally loaded the referencing class. For example, if the virtual machine loads class Volcano through a particular class loader, it will attempt to load any classes Volcano refers to through the same class loader. If Volcano refers to a class named Lava, perhaps by invoking a method in class Lava, the virtual machine will request Lava from the class loader that loaded Volcano. The Lava class returned by the class loader is dynamically linked with class Volcano.
Because the Java virtual machine takes this approach to loading classes, classes can by default only see other classes that were loaded by the same class loader. In this way, Java's architecture enables you to create multiple name-spaces inside a single Java application. Each class loader in your running Java program has its own name-space, which is populated by the names of all the classes it has loaded.
A Java application can instantiate multiple user-defined class loaders either from the same class or from multiple classes. It can, therefore, create as many (and as many different kinds of) user-defined class loaders as it needs. Classes loaded by different class loaders are in different name- spaces and cannot gain access to each other unless the application explicitly allows it. When you write a Java application, you can segregate classes loaded from different sources into different name-spaces. In this way, you can use Java's class loader architecture to control any interaction between code loaded from different sources. In particular, you can prevent hostile code from gaining access to and subverting friendly code.
One example of dynamic extension is the web browser, which uses user-defined class loaders to download the class files for applets across a network. A web browser fires off a Java application that installs a user-defined class loader--usually called an applet class loader-- that knows how to request class files from an HTTP server. Applets are an example of dynamic extension, because the Java application doesn't know when it starts which class files the browser will ask it to download across the network. The class files to download are determined at run-time, as the browser encounters pages that contain Java applets.
The Java application started by the web browser usually creates a different user-defined class loader for each location on the network from which it retrieves class files. As a result, class files from different sources are loaded by different user-defined class loaders. This places them into different name-spaces inside the host Java application. Because the class files for applets from different sources are placed in separate name- spaces, the code of a malicious applet is restricted from interfering directly with class files downloaded from any other source.

By allowing you to instantiate user-defined class loaders that know how to download class files across a network, Java's class loader architecture supports network-mobility. It supports security by allowing you to load class files from different sources through different user-defined class loaders. This puts the class files from different sources into different name-spaces, which allows you to restrict or prevent access between code loaded from different sources.

Millions saw the apple fall, but Newton asked why.
subject: Advantages of User Defined ClassLoader?