• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class loaders

 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we require the custom class loaders?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Ulf Dittmer
    Rancher
    Posts: 42968
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    subodh gupta
    Ranch Hand
    Posts: 203
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can find the API here. Looking up ClassLoader and URLClassLoader is the best way to find out how they are different (or similar).
     
    subodh gupta
    Ranch Hand
    Posts: 203
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Rancher
    Posts: 42968
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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).
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic