load class not in classpath dynamically in web application - without using custom classloader

Posted by swdeveloper on Stack Overflow See other posts from Stack Overflow or by swdeveloper
Published on 2010-03-18T22:04:39Z Indexed on 2010/03/18 22:11 UTC
Read the original article Hit count: 510

Filed under:
|

I am developing a web application.

  1. The web application generates java classes on the fly. For example it generates class com.people.Customer.java
  2. In my code, I dynamically compile this to get com.people.Customer.class and store in some directory say repository/com/people/Customer.class which is not on the classpath of my application server.My application server(I am using WebSphere Application Server/Apache Tomcat etc) picks up the classes from the WEB-INF/classes directory. The Classloader would use this to load the classes.
  3. After compilation I need to load this class so that it becomes accessible to other classes using it after its creation. 4.When I use Thread.currentThread().getContextClassLoader().loadClass(com.people.Customer) obviously the Classloader is not able to load the class, since its not on the classpath(not in WEB-INF/classes). Due to similar reasons, getResource(..) or getResourceAsStream(..) also does not work.

I need a way to :

Read the class Customer.class maybe as a stream (or any other way would do) and then load it. Following are the constraints:

  1. I cannot add the repository folder to the WEB-INF/classes folder.
  2. I cannot create a new Custom ClassLoader. If I create a new ClassLoader and this loads the class, it will not be accessible to its parent ClassLoader. Is there any way of achieving this?

If not this, in the worse case, is there a way of overriding the default class loader with a custom class loader for web applications the same classloader should be used to load applications throughout entire lifecycle of my web application.

Appreciate any solution :)

© Stack Overflow or respective owner

Related posts about classloader

Related posts about classloaders