Blogs

Debugging Liferay 7 In Intellij

Introduction

So I'm doing more and more development using pure Intellij for Liferay 7 / DXP, even debugging.

I thought I'd share how I do it in case someone else is looking for a brief how-to.

Tomcat Setup

So I do not like running Tomcat within the IDE, it just feels wrong.  I'd rather have it run as a separate JVM with it's own memory settings, etc.  Besides I use external Tomcats to test deployments, run demos, etc., so I use them for development also.  The downside to this approach is that there is zero support for hot deploy; if you change code you have to do a build and deploy it for debugging to work.

Configuring Tomcat for debugging is really easy, but a quick script copy will make it even easier.

In your tomcat-8.0.32/bin directory, copy the startup script as debug.  On Windows that means copying startup.bat as debug.bat, on all others you copy startup.sh as debug.sh.

Edit your new debug script with your favorite text editor and, practically the last line of the file you'll find the EXECUTABLE start line (will vary based upon your platform).

Right before the "start", insert the word "jpda" and save the file.

For Windows your line will read:

call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%

For all others your line will read:

exec "$PRGDIR"/"$EXECUTABLE" jpda start "$@"

This gives you a great little startup script that enables remote debugging.

Use your debug script to launch Tomcat.

Intellij Setup

We now need to set up a remote debugging configuration.  From the Run menu, choose Edit Configurations... option to open the Run/Debug Configurations dialog.

Click the + sign in the upper left corner to add a new configuration and choose Remote from the dropdown menu.

Add Configuration Dialog

Give the configuration a valid name and change the port number to 8000 (Tomcat defaults to 8000).  If debugging locally, keep localhost as the host; if debugging on a remote server, use the remote hostname.

Remote Tomcat Debugging Setup

Click on OK to save the new configuration.

To start a debug session, select Debug from the Run menu and select the debug configuration to launch.  The debug panel will be opened and the console should report it has connected to Tomcat.

Debugging Projects

If your project happens to be the matching Liferay source for the portal you're running, you should have all of the source available to start an actual debug session.  I'm usually in my own projects needing to understand what is going on in my code or the interaction of my code with the portal.

So when I'm ready to debug I have already built and deployed my module and I can set breakpoints in my code and start clicking around in the portal.

When one of your breakpoints is hit, Intellij will come to the front and the debugger will be front and center.  You can step through your code, set watches and view object, variable and parameter values.

Intellij will let you debug your code or any declared dependencies in your project.  But once you make a call to code that is not a dependency of your project, the debugger may lose visibility on where you actually are.

Fortunately there's an easy fix for this.  Choose Project Structure... from the File menu.  Select the Libraries item on the left.  The right side is where additional libraries can be added for debugging without affecting your actual project dependencies.

Click the + sign to add a new library.  Pick Java if you have a local source directory and/or jar file that you want to add or Maven if you want to download the dependency from the Maven repositories.  So, for example, you may want to add the portal-impl.jar file and link in the source directory to help debug against the core.  For the OSGi modules, you can add the individual jars or source dirs as you need them.

Add Libraries Dialog

Conclusion

So now you can debug Liferay/Tomcat remotely in Intellij.

Perhaps in a future blog I'll throw together a post about debugging Tomcat within Intellij instead of remotely...

Thank you for sharing!
I'm also running IntelliJ and not running Tomcat within IntelliJ.
>The downside to this approach is that there is zero support for hot deploy; if
>you change code you have to do a build and deploy it for debugging to work.
JRebel will do the work for you.

For debugging, I changed my setenv.sh (in tomcat/bin), and added the following line:
DEBUG_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"
then DEBUG_OPTS is added to JAVA_OPTS:
JAVA_OPTS="$JAVA_OPTS $DEBUG_OPTS ... etc.
I haven't tried jrebel against LR7 yet. Have you been able to change code, jsp, etc in the module layer and get it to work? If so, I'll have to give it a shot...

I went with the "jpda start" option so I didn't have to put the debug settings in setenv.sh. I want to be able to switch debug on and off w/o having to edit setenv.sh. Using the already supported "jpda start" option was just the ticket.
please show us the embedded / integrated liferay runtime tomcat JVM with intellij; do you know that only intellij can do integrated tomcat running and debugging on a separate host, not just local? key differentiator between eclipse and intellij (being the superior IDE)
I prefer IntelliJ configs for run/debug of Tomcat with or without JRebel. No idea how JRebel will be used with Liferay 7 OSGi jars.
Ok, I gave in to David's wisdom. I now follow similar to what David presents here where I run tomcat and then debug from IntelliJ. This approach allows my dev tomcat to run in a more similar way to non-dev environments like staging and prod.
I wouldn't call it wisdom, really. It is really more of a reflection of having all to often given something that worked just fine in a developer rig only to find it fail on me for one reason or another when deployed to test.

So now I always run my local dev environment as a clone of how it is in test; separate elastic, manual copying of artifacts to the Liferay deploy folder, ...

I know it is the old school way of doing things, and it does cost me some developer time. But my artifacts deploy just as I expect in test and higher lanes, so to me it is a price worth paying.
how would I add in debugging of marketplace jasperreports-web ? is EE only and source is available
Good question. Not sure I know. Intellij does have a decompiler for when source is not available, but I don't know how you would set your original breakpoints, for example. Surely it must be possible, of course, but it would take some investigation to find out.

Thanks for sharing.

Is there a way to debug JSPs inside OSGi modules in Liferay 7?

https://community.liferay.com/forums/-/message_boards/message/112440770