0

In one of our production environment we have a messed up structure, The tomcat WEB-INF has ../classes with configurations and ../lib with jars.

We find that the classes directory has a directory following the package structure as below, Sample:

com/test/A.class

Within the lib directory also we have another jar with the same class packaged.

The real issue was looked into when we found that duplicate processing is happening though it is not sure if the issue is because of this.

The actual question is when tomcat starts will two instances of the same class gets created and parallel processing happen? Is it a possibility because both classes directory and lib directory is in the classpath.

Sijo Kurien
  • 95
  • 2
  • 10
  • Depends on which ClassLoader is being used. One full qualified class name is unique per ClassLoader. Yes, in theory you could have loaded "com.example.Foo" multiple times in a single application – Felix Jun 30 '20 at 08:05

1 Answers1

1

It is not about tomcat but classloaders. The hierarchy is BootstrapLoader(rt.jar)->ExtensionClassLoader(java.ext.dir location)->ApplicationClassLoader(from application -classpath parameter). So, if the class is loader by any loader from higher hierarchy, it will not get loaded again from any other jar. If you want to load a class from a particular jar without changing the classloading order, please refer to How to load Classes at runtime from a folder or JAR?

KPS
  • 31
  • 2
  • If there are no customisations and we use tomcat as original, is there any possibility of being loaded multiple times? – Sijo Kurien Jul 01 '20 at 13:14
  • It is only possible if the class is there in application loader and more than one application in tomcat. – KPS Jul 01 '20 at 13:20