This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes class loaders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "class loaders" Watch "class loaders" New topic
Author

class loaders

subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
why we require the custom class loaders?


http://subodh-gupta.blogspot.com
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Creating your own custom classloader is an advanced subject. I've never had to write my own classloader. But I can imagine it could be necessary when you want to load classes in a nonstandard way, or if you need to control certain aspects such as to which libraries the loaded classes have access.

Tomcat for example has its own custom classloader for web applications, which gives each webapp access to the common Tomcat libraries and to the classes in the WEB-INF/classes directory and the libraries in the WEB-INF/lib directory of the webapp.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Jesper Young:
Creating your own custom classloader is an advanced subject


Nah, it's not that hard. Well, not necessarily - it depends what you want your custom classloader to do.

The reasons I've had to write custom class loaders are: -

  • to fetch the class data from an unusual place, such as a SQL database
  • to apply simple encryption/decryption, as a deterrent to hacking and reverse engineering


  • I'm sure there are other reasons, too.


    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41137
        
      45
    I've used them to load classes from places outside of the classpath (e.g. a plugins directory for user-added classes), and for having a security manager associated with those classes, so that they're not allowed everything the hosting application is allowed to do.


    Ping & DNS - my free Android networking tools app
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Was the original question about writing your own ClassLoader class or using custom instances of the existing classes? I've never had to write one, but have made new URLClassLoader instances with modified classpaths.

    JUnit uses a classloader trick to reload classes every time you run a test. I don't know if they have custom code or just an instance of URLClassLoader. It's Open Source so you can go take a look.

    I used a monitoring tool that inserts bytecode at class load time. They "log" every method entry and exit so they can report invocation counts and performance.


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    subodh gupta
    Ranch Hand

    Joined: Jul 23, 2007
    Posts: 203
    Whats the difference between URLClassLoader and ClassLoader?

    can anybody provide me with a link on this topic so that i can a knowledge i am looking for...

    and i can ClassLoader thing will help in CodeCovrage tool?
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    You can find the API here. Looking up ClassLoader and URLClassLoader is the best way to find out how they are different (or similar).


    "I'm not back." - Bill Harding, Twister
    subodh gupta
    Ranch Hand

    Joined: Jul 23, 2007
    Posts: 203
    I have gone through the doc of the two and think ClassLoader gives you more power than the URLClassLoader.

    And they also differ in the way security manager is set.

    Am i right?

    If yes can you tell me how to start on building the custom class loader because i want to see how it works.

    And can i retrive the code back from the bytecode?
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41137
        
      45
    Originally posted by subodh gupta:
    I have gone through the doc of the two and think ClassLoader gives you more power than the URLClassLoader.

    The main difference is where they load classes from. CL looks at the classpath, while URLCL retrieves them from a URL.

    If yes can you tell me how to start on building the custom class loader because i want to see how it works.

    The http://faq.javaranch.com/java/SecurityFaq links to two articles about classloaders, one of which shows how to build one.

    And can i retrive the code back from the bytecode?

    ClassLoaders generally handle byte code only; if you want the source code to a class look into decompilers. Keep in mind that decompiling is not allowed in many cases (e.g. commercial code).
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: class loaders
     
    Similar Threads
    Changes on JSP
    True/False: Unloaded instances
    PermGen error
    Singleton implemented using Servlets
    when a user-defined class loader is required??