« 返回

Debugging Liferay in Eclipse

Company Blogs 2010年9月22日 按 Ray Augé Staff

When should you use a debugger during development? At ALL times! At least that's my view.

Isn't that a hassle? Well debugging sure can be, but if you setup your environment in a specific way, it can actually become very fast and almost transaprent. I like transparent!

Here are steps I take to configure my tomcat (I've followed the same steps with JBoss):

  1. Note the deployment path of tomcat (in my case I'll use /bundles/tomcat-6.0.x)
  2. In Eclipse, open the "Run Configurations" manager (Run -> Run Configurations...)
  3. On the "Java Application" node, right click and choose "New".
  4. On the first tab (Main) select the project you are running in tomcat
  5. In the Main Class field enter org.apache.catalina.startup.Bootstrap
  6. On the Arguments tab, in the Program Arguments field enter start
  7. In the VM arguments field enter the following:

    -Xms1024m
    -Xmx1024m
    -XX:PermSize=128m
    -XX:MaxPermSize=256m
    -XX:+CMSClassUnloadingEnabled
    -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=true
    -Dcatalina.base=/bundles/tomcat-6.0.x
    -Dcatalina.home=/bundles/tomcat-6.0.x
    -Djava.io.tmpdir=/bundles/tomcat-6.0.x/temp
    -Dexternal-properties=${workspace_loc:<NAME_OF_YOUR_ECLIPSE_PROJECT>}/portal-web/docroot/WEB-INF/src/portal-developer-tomcat.properties
    -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
    -Djava.util.logging.config.file=/bundles/tomcat-6.0.x/conf/logging.properties


    You many notice that I included a reference to my portal project's portal-developer-tomcat.properties file. This way I can change portal settings and only need to restart the portal without a redeploy, which is very convenient.

    You may also notice that I included tomcat's juli log manager so that all the normal web app configurations work as expected and in the Eclipse console, which is also very nice.

    Adjust your heap settings as desired, but the above settings have worked for me in 99.999% of cases.
     
  8. In the Working directory field choose "Other" and enter /bundles/tomcat-6.0.x/bin. Notice that this is the tomcat path, plus the /bin folder.
  9. The JRE tab should be already set properly, but you can choose any JRE that is installed that is 1.5+.
  10. On the Classpath tab, remove any entries under User Entries and then choose "Add External JARs...".
  11. Select the 3 jars located in /bundles/tomcat-6.0.x/bin.
  12. On the Source tab make sure that you have both Default, as well as your project in the list.

    Optionally here you can add any plugin projects that you are working on, as well as adding the path to the jsp java classes (typically /bundles/tomcat-6.0.x/work/Catalina/localhost/_)
  13. On the Common tag choose at least Debug and Run under "Display in favorites menu".
  14. Click "Apply" near the bottom of the dialog.
  15. Finally, to start the portal in debug mode, click the Debug menu in the Eclipse toolbar, and choose the run config you jsut created.

    You should see at this point output on the Eclipse console showing that tomcat is starting up.

I've been using this techinque for at least 3 years and I run the portal this way 99.999% of the time. By doing so, I can at any point in time add a breakpoint and start trouble shooting and I don't have to worry about re-connecting to or restarting the portal in debug mode, I know it's already done.

If you want to add any other java apps this way, the simples technique is to first run the app in the traditional way, then observe the process log to see what the full command that was actually used to start it, and from where. Once you know that, you can add any app to Eclipse Run Configuration.
 

 

讨论主题回复 作者 日期
Hey Ray, I use to work in a different and IMO... Thiago Leão Moreira 2010年9月22日 上午9:38
I started this way long ago. I dropped it... Ray Augé 2010年9月22日 上午9:50
@Ray Can you explain step by step your... Denis Signoretto 2010年9月24日 上午3:46
Besides the configuration above, can you... Ray Augé 2010年9月24日 上午7:47
@Ray Hi ray, you are right I was't really... Denis Signoretto 2010年9月24日 上午9:38
Got it! I simply meant "the method that I... Ray Augé 2010年9月24日 上午9:43
nice post! Thank you, Ray Jonas Yuan 2010年9月22日 上午11:45
Hi Ray! It's good to see this stuff written... Vilmos Kozma 2010年9月23日 上午6:53
You can do hot code replacement using this... Ray Augé 2010年9月23日 上午8:39
How should i hot-code replace classes? I know... Corné Aussems 2011年3月18日 下午1:33
Eclipse should do it automatically. Of course... Ray Augé 2011年3月18日 下午1:38
Oh, as I learned the hard way last week, jsp... Ray Augé 2011年3月18日 下午1:39
Hi, you can combine your ideas. Extend VM... Daniel Niklas 2010年11月11日 上午1:17
It works GREAT! Thanks! Tamas Imrei 2010年11月16日 上午2:33
Since I can't see my portlet on the website, it... Its Me 2011年1月24日 上午8:57
I just found out by trial that when in the... Corné Aussems 2011年3月28日 下午2:26
Just testing and got an error using helios SR2 ... Michel Wicky 2011年5月14日 上午12:35
You still seeing this? I wonder if eclipse... Ray Augé 2011年5月17日 上午6:25
I updated it to just say... Ray Augé 2011年10月24日 上午7:14
hi sir. pls tel me how to configure liferay... Babu Reddy K V 2012年3月22日 上午4:13
I would do basically the same thing as above... Ray Augé 2012年3月22日 上午6:12
Creating my project of, 'portal', with the,... Linus Sphinx 2012年4月11日 下午2:22
Does this method of debugging in Eclipse no... Mary Mizrahi 2012年5月24日 下午5:59
I have no idea with respect to the IDE, but I... Ray Augé 2012年5月25日 下午12:37
You are a genius !!!! Thx Jacques Traore 2013年2月5日 下午12:38
this method is debug for portlet, theme, ext,... hiep lq 2013年6月11日 下午9:04
This method works for the portal, but you need... Ray Augé 2013年6月12日 上午10:53

Hey Ray, I use to work in a different and IMO more efficient way. I start my Tomcat in a remote debug mode, just call from command line the command: "catalina.sh jpda run" . Then you can connect your Eclipse to Liferay using the "Debug Configurations > Remote Java Application" menu. By default the host and port points to the correct place. You can also disconnect as you wish and continue the normal testing. Give a try and let me know.

um abraço!
在 10-9-22 上午9:38 发帖。
I started this way long ago. I dropped it because it caused me to have to editing the tomcat configurations all the time, setting different VM settings, etc. With the method I use, I can duplicate the run config, and change only a few settins, such as adding JMX, or particular GC settings, etc., all without touching the files in the bundle. I can even switch to different installed JVMs configured in Eclipse without having to edit more config, or change the system PATHs or settings.
在 10-9-22 上午9:50 发帖以回复 Thiago Leão Moreira
在 10-9-22 上午11:45 发帖。
Hi Ray!

It's good to see this stuff written down. However, for the whole picture it would be nice to hear about hot code replacement. When I debug my code and find something to be fixed I like not having to run any build script to test the new code. So is the above description solves this issue or we need to do some extra work to get the JVM to hot code replace?
在 10-9-23 上午6:53 发帖。
You can do hot code replacement using this technique. There is of course the limitation of the (Sun) JVM, which does not allow new|changes to the class's public signature.

Oh, and the project does have to be listed in the sources tab of the run configuration.
在 10-9-23 上午8:39 发帖以回复 Vilmos Kozma
@Ray

Can you explain step by step your method?

Thanks,
Denis.
在 10-9-24 上午3:46 发帖以回复 Ray Augé
Besides the configuration above, can you explain a little what "method" you'd like me to clarify? I'm not quite sure what you're asking, but I would like to give a good answer.
在 10-9-24 上午7:47 发帖以回复 Denis Signoretto
@Ray

Hi ray, you are right I was't really clear. I'm referring to your sentence "With the method I use". I 'm guessing it's the same of Thiago.

Thanks,
Denis.
在 10-9-24 上午9:38 发帖以回复 Ray Augé
Got it! I simply meant "the method that I described above in the blog post".
在 10-9-24 上午9:43 发帖以回复 Denis Signoretto
Hi,

you can combine your ideas. Extend VM arguments to
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

So you get the advantages metioned by Ray and you can use "Remote Debugging" as Thiago has described.

Daniel :-)
在 10-11-11 上午1:17 发帖。
在 10-11-16 上午2:33 发帖。
Since I can't see my portlet on the website, it isn't deployed to the server? How can i do this in eclipse?
在 11-1-24 上午8:57 发帖。
How should i hot-code replace classes?
I know there is deploy-fast for jsps and deploy.properties.
在 11-3-18 下午1:33 发帖以回复 Ray Augé
Eclipse should do it automatically. Of course you can't add new methods, but changes should be pushed automatically as soon as you save, while in debug.

Just make sure to include their path to the jsp java source in the run configuration (that is in the "work" folder, /work/Catalina/localhost/*/).
在 11-3-18 下午1:38 发帖以回复 Corné Aussems
Oh, as I learned the hard way last week, jsp pre-compile breaks the ability to debug them, so don't do that.

They have to be compiled inside the debugged JVM.

I even tried pre-compiling them with debug flags added, but it still didn't work. Anyone with insight in that please let me know.
在 11-3-18 下午1:39 发帖以回复 Ray Augé
I just found out by trial that when in the Servers View you add a Tomcat Server or Liferay Server (IDE/Studio) and point it to your newly build bundle and just under Open launch configuration >> Source >> Add : Java Project >> The portal project.
Start in debug and voila..
在 11-3-28 下午2:26 发帖。
Just testing and got an error using helios SR2

Variable references non-exitent resource: ${workspace_loc:portal}
在 11-5-14 上午12:35 发帖以回复 Corné Aussems
You still seeing this? I wonder if eclipse changed the default variable names!
在 11-5-17 上午6:25 发帖以回复 Michel Wicky
I updated it to just say ${workspace_loc:<NAME_OF_YOUR_ECLIPSE_PROJECT>}

Replace that token with whatever is the name of your eclipse project. OR just replace that entire variable with the absolute path to your project.
在 11-10-24 上午7:14 发帖以回复 Ray Augé
hi sir.
pls tel me how to configure liferay with Jboss to eclips
在 12-3-22 上午4:13 发帖。
I would do basically the same thing as above and get the start command used by jboss, and setup the run config something like this:

http://i.imgur.com/Gjqyv.png
http://i.imgur.com/joTTR.png
http://i.imgur.com/­m8mY5.png
http://i.imgur.com/6wZTX.png

Note the ${JBOSS_510_DEPLOY} Eclipse variable points to the JBoss install location.
在 12-3-22 上午6:12 发帖以回复 babu reddy
Creating my project of, 'portal', with the, "Create from ant build file", option, all works, see the app, create the run configuration and fire i
t up just fine but I don't see any source to step through, just all the jars. How do you create your project in eclipse?
在 12-4-11 下午2:22 发帖。
Does this method of debugging in Eclipse no longer work with 6.1 and IDE 1.52?
For some reason I no longer get output to the console from my portlets and break points are ignored.
在 12-5-24 下午5:59 发帖以回复 Linus Sphinx
I have no idea with respect to the IDE, but I still use this same technique with 6.1 and trunk.

Have you included the plugin projects in the sources list of the debug configuration?
在 12-5-25 下午12:37 发帖以回复 Mary Mizrahi
在 13-2-5 下午12:38 发帖以回复 Ray Augé
this method is debug for portlet, theme, ext, hook project?
when i use liferay. I use web-content and cms script.
my code find service is return nothing. i want debug into serviceLocator.findService.
howto setup enviroment for debug portal code?
在 13-6-11 下午9:04 发帖。
This method works for the portal, but you need the source to make it useful.
在 13-6-12 上午10:53 发帖以回复 hiep lq