0

I am working a problem where multiple applications need to share classes loaded by each other.

So I have started by creating a separate application and putting all the common libraries in it. I have also made necessary app server specific enablements/configuration changes. I am trying to find out its possible to load all classes under WEB-INF/lib/*.jar as soon the war is deployed.

Assume I don't want individually load classes by using i.e. Foo.class. I am really trying to see if just all classes under WEB-INF/lib could be loaded manually either by enabling configurations are writing JAVA code.

Arjan Tijms
  • 36,666
  • 12
  • 105
  • 134
Sundar Annamalai
  • 2,080
  • 2
  • 14
  • 17
  • And what is your question / problem? – Leos Literak Jan 22 '14 at 18:36
  • Did you have a look at the link bellow, with a way to load classes at startup, also if you could let us know the reason for doing it so we better understand the use case, but in general it is not an advisable approach and there could be better alternatives depending on the reason why this is needed – Angular University Jan 23 '14 at 23:18
  • Apparently this is not required now. I thought i have to manaully load classes in order for ClassLoaders to pickup from shared classloading domain. Thats not the case. – Sundar Annamalai Jan 24 '14 at 04:28

2 Answers2

1

If you are trying to share libs within multiple web apps you need to let these libs inside JBOSS's shared library folder. JBOSS will load then at startup.

See this:

Where to put a shared library in JBoss AS 5?

It's not possible for a webapp access another webapp classloader. The app classloader, has access to 3 classloaders:

  1. Its own classloader
  2. Container classloader
  3. Bootstrap classloader(which loads the Java SDK classes).
Community
  • 1
  • 1
edubriguenti
  • 3,488
  • 3
  • 31
  • 43
  • 1
    Actually JBoss does assist in sharing classes between multiple apps. Check http://phytodata.wordpress.com/2010/10/21/demystifying-the-jboss5-jboss-classloading-xml-file/ – Sundar Annamalai Jan 23 '14 at 04:05
1

If we go through the different points you mentioned:

  • I am working a problem where multiple applications need to share classes loaded by each other: put the classes in a common classloader at the server level, and remove them from the WARs WEB-INF/libs to avoid that WAR classes hide server classes. For example in the case of Tomcat put the jars in $TOMCAT_HOME/lib.

  • why this not a good idea: This is usually not a good idea and we should only put at the server level the minimum amount of classes needed, to avoid problems due to static variables (linked to the class) to be unintentionally shared accross applications, increased possiblity of class cast exceptions, etc.

  • So I have started by creating a separate application and putting all the common libraries in it: This won't work as you expect, the classes in one WAR application's WEB-INF/lib are isolated and not visible in classes in another WAR. So creating a separate application is not the way to achieve this, but installing the classes at a classloader at the server level.

  • I am trying to find out its possible to load all classes under WEB-INF/lib/.jar as soon the war is deployed:* This is not foreseen in servlet containers, see this answer for a way to do this using Java code. The reason this feature is not available is that the JVM loads the classes lazily when they are needed by other classes, and no upfront loading is done in order to save memory.

The moment when the loading of the classes occurs is best left to the inner workings of the JVM, where many optimizations are in place to load classes in the most efficient way and avoid perm gen issues.

If this does not answer the questions can you let us know what your use case is for needing to load classes upfront, we might be able to provide some alternative for what you are trying to implement - like increasing timeouts for initial requests, etc.

Community
  • 1
  • 1
Angular University
  • 38,399
  • 15
  • 70
  • 79