« Back

Classloading in Liferay

General Blogs March 20, 2014 By Peter Mesotten

Libraries (or JAR-files if you will) exist in many different locations in your Liferay installation. Some reside in Tomcat's lib folder, others are included in the lib folder of Liferay's ROOT. And of course, there are the libraries that are included in your own portlets. It's no secret that Liferay performs some magic tricks to enable access to different classes in different contexts. This complexity is mostly abstracted away from developers, so you don't have to worry if class X is indeed available for you from within a portlet. However, there are some very specific particularities that you need to be aware of as a Liferay developer. Welcome to the wonderful world of classloading in Liferay!

By the way, understanding the principles in this article might help you in certifying for Liferay, as you're guaranteed to get some questions on this topic in your certification exam!

Classpaths in Liferay

Roughly speaking, 3 different classpaths are active in a Liferay installation.

The global classpath includes all JAR files that are defined in Tomcat's lib and lib/ext folders. All deployed webapps (including ROOT and your own portlets and hooks) have access to these libraries. Examples of included libraries are servlet-api.jar (Servlet API), portlet.jar (Portlet 2.0 API) and portal-service (Liferay Service API). Because they are already available, you don't want to include these libraries in your own portlet or hook. Otherwise, the library will be loaded twice, which in most cases will result in deploy failure.

The portal classpath includes all JAR files from the lib folder of ROOT. These 200+ libraries are only accessible from within the ROOT application itself. Examples of included libraries are portal-impl.jar (Liferay Implementation), util-java.jar, util-bridges.jar, util-taglib.jar, Spring libraries, ...  

The portlet/hook classpath includes all JAR files from the lib folder of the deployed portlet or hook. These libraries are only accessible from within the portlet or hook itself.

Who can access what?

With all those different classpaths, it's important to know which libraries are available in which context.

Inside Liferay's ROOT, classes and JSPs can only access the global and the portal classpath.

Inside a portlet, you have access to both the global and the portlet classpath. This means that both the global libraries and the portlet libraries are loaded together. Or why it is extremely important never to include libraries in your portlet that are already available in the global classpath! If you're using Maven and you still want to compile against those libraries in the global classpath, put the scope of your dependency to provided.

Inside a hook the same rules apply as for portlets. Most hooks have access to both the global and the hook classpath. There is however one exception: JSP hooks. At deploy time, JSPs that are overriden in a JSP hook replace the original JSP in the ROOT. At this point, the JSP can only access the global and portal classpaths, NOT the hook classpath. For instance, it is impossible to let an overridden JSP access a class that's contained in a third-party JAR that's included in the lib folder of the hook.

Sharing libraries between portlets/hooks

If you've been paying attention, you'll agree with me that the only way to share libraries between different portlets is to add those libraries tot the global classpath, i.e. in Tomcat's lib or lib/ext folder. However, generally, this is not a great idea. At least, you should make sure that those libraries don't end up to be duplicated in different classpaths. An example.

Suppose all of your portlets make heavy use of the Spring framework. To not include libraries like spring-context.jar, spring-beans.jar, spring-core.jar in every portlet, you decide to add them to the global classpath in tomcat/lib/ext. But, oh my, suddenly your portal stops working. Why? Because Liferay's ROOT already includes these Spring libraries, and now Liferay tries to load those libraries twice for the ROOT application. Result: conflicts!

If you still wish to share libraries through the global classpath, take the following thoughts into account:

  • Make sure the libraries don't already exist on the portal classpath. If so, forget about sharing these libraries and include them in every portlet or hook that needs them.
  • Make sure the libraries are not included in any of the deployed portlets or hooks. If so, set the scope of these dependencies to provided instead of compile, so they are not packaged in the WAR file.
  • Prepare to lose some amount of flexibility. Indeed, all your portlets and hooks will now have to commit to the same version of the library you're adding. This can be good for consistency, but do you really want to recompile all existing portlets and hooks if you suddenly need to use the latest version of library X in one of them?

Dependency JARs

Liferay does provide a mechanism to gain access to classes in the portal classpath while inside your portlet by the means of portal dependency JARs. This is a property you can set in the liferay-plugin-package.properties configuration file. It accepts a list of JAR files that are to be found in the portal classpath.

WEB-INF/portlet-plugin-package.properties
name=sample-portlet
module-group-id=liferay
module-incremental-version=1
...
portal.dependency.jars=commons-logging.jar,commons-io.jar

At deploy time, Liferay will copy over all matching JARs from the portal's lib folder to the lib folder of the portlet or hook. So you don't actually get access to the portal classpath. Instead, the necessary JAR files are copied over to the portlet/hook classpath.

In some rare cases, it can be useful to define these dependency JARs. Some framework JARs are enhanced by Liferay to enable some specific behavior. And by including the JAR this way, you're sure you have the exact same implementation version of the library. But in general, try to avoid this type of configuration as it tends to create more problems than it solves. If you're using it anyway and your compilation units depend on those JAR's, don't forget to specify these dependencies as provided in Maven. Otherwise, they will be added twice to the portlet's lib folder.

About portal-impl.jar

Being able to include dependency JARs from the portal context into your own portlet or hook effectively allows you to depend on portal-impl.jar in your plugin. Don't. Do. This. The portal-impl.jar library is not meant to be exposed to custom plugins. That's the whole point of having an API (portal-service.jar). And remember what we said about portal-service.jar: you don't explicitely add this library to your portlet as it's already available in the global classloader.

Evaluation time!

If you can solve these questions, you've totally mastered the classloading concepts in Liferay. Feel free to post a comment if you have more questions on the topic.

  1. I created a JSP hook that uses Apache POI to export the list of user groups to Excel through a new button in the UI. Which of the following approaches will work if you know that poi.jar is included in ROOT/WEB-INF/lib?
    1. Add poi.jar to the lib folder of my JSP hook
    2. Add poi.jar to the list of portal.dependency.jars in the portlet-plugin-package.properties file of my JSP hook
    3. Add poi.jar to the lib/ext folder of Tomcat
    4. None of the above
  2. I created a service hook that uses POI to export the list of user groups to Excel each time a new user group is created. Which of the following approaches will work? Multiple answers are possible
    1. Add poi.jar to the lib folder of my hook
    2. Add poi.jar to the list of portal.dependency.jars in the portlet-plugin-package.properties file of my hook
    3. Add poi.jar to the lib/ext folder of Tomcat
    4. None of the above
  3. My portlets always use the Vaadin framework. Which of the following approaches will work if you know that vaadin.jar is included in ROOT/WEB-INF/lib? Multiple answers are possible.
    1. Add vaadin.jar to every portlet's lib folder separately
    2. Add vaadin.jar to the lib/ext folder of Tomcat
    3. Add vaadin.jar to the lib/ext folder of Tomcat and remove vaadin.jar in ROOT/WEB-INF/lib
    4. Add vaadin.jar to the list of portal.depency.jars in the portlet-plugin-package.properties file of my portlets

Visit http://blogs.aca-it.be for more blogs on Liferay and Java.

Threaded Replies Author Date
Great article Peter !! Can you confirm answers... Rafik H March 20, 2014 4:50 PM
@Rafik: That is correct. You passed the exam! :-) Peter Mesotten March 21, 2014 3:27 AM
Good :) Rafik H March 24, 2014 3:48 AM
Why not option 'c' in Questions 1 and 2? Portal... Kailash Yadav March 25, 2014 6:51 AM
I got the answer :) I was wrong :P Kailash Yadav March 25, 2014 6:54 AM
Because the JAR is already in ROOT's lib directory Luboš Vrba September 18, 2015 6:25 AM
Does silence of Peter implies his consent? ;-)... Michal Sima January 8, 2015 8:18 AM
Does silence of Peter implies his consent? ;-)... Michal Sima January 8, 2015 8:18 AM
@Michal, as I've confirmed before in this... Peter Mesotten January 9, 2015 12:44 AM
Nice Explanation! Harish Kumar March 20, 2014 11:52 PM
Great Article with nice coverage of Liferay... Tejas Kanani March 21, 2014 2:10 AM
Nice write up.. Got a clear understanding of... Balaji Murugesan April 2, 2014 8:28 AM
What versions of Liferay is this true? Are the... Tom Wilbourn April 4, 2014 11:31 AM
@Tom - The principles are not bound to a... Peter Mesotten April 6, 2014 4:26 AM
Wonderful article Peter, a really "basic"... Niccolò Pasquetto May 3, 2014 3:36 AM
Thank you for this article. AYED Chekib May 3, 2014 8:57 AM
HI Peter, Thanks for your nice article, but... cao wei July 22, 2014 2:32 AM
Hi, Excellent question! Indeed, the servlet... Peter Mesotten July 24, 2014 1:12 AM
@Peter ........... I need a bit of info. I... Arunjyoti Banik January 11, 2015 1:35 PM
@Arunjyoti, I'm not sure what you're trying to... Peter Mesotten January 12, 2015 12:42 PM
@Peter ........... I solved it. I had wrongly... Arunjyoti Banik January 12, 2015 12:51 PM
Great explanation !!!!!! arti gupta March 26, 2015 11:16 PM
For the first question answer->d Vignesh Kumar May 5, 2015 3:40 AM
Can anyone explain what will happen if put the... Vignesh Kumar May 5, 2015 3:46 AM
great! :D John Voltaire Maximo August 26, 2015 8:44 PM
[...] David H Nebinger: There's a lot of class... Anonymous January 20, 2016 5:36 AM
[...] Olá pessoal, tudo bem? eu estou com... Anonymous February 15, 2016 10:49 AM
[...] There is no shortcut. Don’t rely on any... Anonymous August 9, 2017 2:49 PM

Great article Peter !!
Can you confirm answers for the questions:
1 -- d : because jsp hook are runnig in portal classloader.
2 -- a, b : because service hook is running in hook's classloader.
3 -- a, c, d
Posted on 3/20/14 4:50 PM.
Posted on 3/20/14 11:52 PM.
Great Article with nice coverage of Liferay Classloading principles !!!
Posted on 3/21/14 2:10 AM.
@Rafik: That is correct. You passed the exam! :-)
Posted on 3/21/14 3:27 AM in reply to Rafik H.
Posted on 3/24/14 3:48 AM in reply to Peter Mesotten.
Why not option 'c' in Questions 1 and 2? Portal and portlets also have access of lib/ext (global classpath). Please currect me if I am wrong?
Posted on 3/25/14 6:51 AM in reply to Rafik H.
I got the answer emoticon I was wrong emoticon
Posted on 3/25/14 6:54 AM in reply to Kailash Yadav.
Nice write up.. Got a clear understanding of Liferay's classpath
Posted on 4/2/14 8:28 AM.
Tom Wilbourn
What versions of Liferay is this true? Are the jars loaded from the temp directory if running in tomcat? We've also had issues where one portlet had an older version of a dependency and this was loaded in the classpath for a different portlet which had a newer version of the dependency instead of the newer version. This really caused a problem. Our dependencies are managed by maven and the jars include the version in the names.
Posted on 4/4/14 11:31 AM.
@Tom
- The principles are not bound to a certain Liferay version. However, between different application servers there might be small variations. E.g. Jboss packages a lot more JAR files in the "global" classpath than Tomcat.
- The JAR files are never loaded from temp. Maven imposes a problem indeed if you upgraded a dependency but didn't do a clean deploy. A clean deploy means, deleting the portlet/hook from the webapps folder of Tomcat before redeploying the WAR file. In our Liferay team, this is the standard procedure for deploying portlets and hooks.
Posted on 4/6/14 4:26 AM in reply to Tom Wilbourn.
Wonderful article Peter, a really "basic" article.

If I can afford a hint, in question number 2 I would repeat that poi.jar is included in ROOT/WEB-INF/lib.
Posted on 5/3/14 3:36 AM.
Thank you for this article.
Posted on 5/3/14 8:57 AM.
HI Peter,
Thanks for your nice article, but i'm confused when i share services among multiple portlets, in this case portlets seems can access other portlet's implementation class. This conflicts with your article that war can't see each other in liferay.

Example:
I have a Plugin common-service.war, in this plugin, i use liferay service builder to generate the service layer, and separate it into common-service.jar and common-service-impl.jar (those *impl.class)

and i have another plugin myPortlet.war, i defines required-deployment-contexts=common-service and include common-service.jar in the myPortlet.war's library. This way, myPortlet can invoke the services provided by common-service.war. Please note that common-service-impl.jar is NOT included in the myPortlet.war.

As two war files can't see each other during runtime, why myPortlet.war can invoke the service from common-service.war, the implementation class common-service-impl.jar is not available to myPortlet.war?
I guess liferay uses some special class loading for this scenarios?

Peter, any advice on this?
Posted on 7/22/14 2:32 AM.
Hi,

Excellent question! Indeed, the servlet specification doesn't allow WAR files to see each other's classes. Therefore the classloader of portlet X would normally not be accessible for portlet Y.

However, Liferay circumvents this by introducing classloader proxies (CLPs). So I guess you're right that Liferay performs some magic to make this work. For your reference, this link describes the principle more in depth: http://agile-reflections.opnworks.com/2012/04/sharing-classes-between-liferay-pl­ugins.html
Posted on 7/24/14 1:12 AM in reply to cao wei.
Does silence of Peter implies his consent? ;-) Are those answers correct?
Posted on 1/8/15 8:18 AM in reply to Rafik H.
Does silence of Peter implies his consent? ;-) Are those answers correct?
Posted on 1/8/15 8:18 AM in reply to Rafik H.
@Michal, as I've confirmed before in this thread the answers of Rafik are correct indeed :-)
Posted on 1/9/15 12:44 AM in reply to Michal Sima.
@Peter ........... I need a bit of info. I created a DefaultLandingPageAction.java through a hook plugin inside a package com.hook.test.path. I just took the sourcecode of this file and introduced two variables there so that they can be used in setting landing page path in portal-ext.properties. I also added these two properties::
override.default.landing.page.path=true;
login.events.post=com.lifera­y.portal.events.ChannelLoginPostAction,com.hook.test.path.DefaultLandingPageActi­on,com.liferay.portal.events.LoginPostAction.

As usual I had the classloading problem, since my java file isnt in portal-impl.jar. I just want to know what else I have to do? And what is the use of overiding the path variable mentioned here:: https://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Customizing%20the%2­0default%20page%20after%20login.
Posted on 1/11/15 1:35 PM.
@Arunjyoti,

I'm not sure what you're trying to accomplish in your custom landing page action, but in 99% of the cases you can work around a dependency on classes from portal-impl.jar. Could you post (or PM) the code for your DefaultLandingPageAction, so I can take a look and see where internal references could be replaced by service-level references.
Posted on 1/12/15 12:42 PM in reply to Arunjyoti Banik.
@Peter ........... I solved it. I had wrongly made my class under Struts Action. And with the code I modified, I can land into the group page, a user is assigned.

Now I am working on which group page a user wants to land if he is assigned to multiple groups, based upon his choice.
Posted on 1/12/15 12:51 PM in reply to Peter Mesotten.
Great explanation !!!!!!
Posted on 3/26/15 11:16 PM.
For the first question answer->d
Posted on 5/5/15 3:40 AM.
Can anyone explain what will happen if put the jar file in both location tomcat/lib/ext and root/web-inf/lib.I am new to liferay.It will be much helpful for me if i understand this...Please Response quick
Posted on 5/5/15 3:46 AM in reply to Vignesh Kumar.
Because the JAR is already in ROOT's lib directory
Posted on 9/18/15 6:25 AM in reply to Kailash Yadav.
[...] David H Nebinger: There's a lot of class loader magic that goes on under the hood. I've kinda avoided peeling back those layers, I don't really want to see what's behind that curtain... Thanks... [...] Read More
Posted on 1/20/16 5:36 AM.
[...] Olá pessoal, tudo bem? eu estou com vários portlets que utilizam o hibernate, e ate então as lib's do hibernate estão dentro de cada portlet. Todos os portlets funcionam muito bem. O problema é o... [...] Read More
Posted on 2/15/16 10:49 AM.
[...] There is no shortcut. Don’t rely on any sample questions online(including this post) completely. Best way to prepare is to practice tutorial here . Even the experienced developers should try to go... [...] Read More
Posted on 8/9/17 2:49 PM.