配置Tomcat和Eclipse集成调试来代替hot deploy热部署

JVM有一个 Java 平台调试架构( Java Platform Debugger Architecture , JPDA )支持Java程序调试,也就是说DEBUG调试与IDE开发工具无关,可以在eclipse, Intelli-J IDEA,netbeans进行DEBUG调试,JPDA也和应用服务器无关,可以在Weblogic,Jboss,Tomcat里进行DEBUG调试。

DEBUG支持实时代码修正,JPDA 实现了在运行中的应用程序中用经过修改的代码进行替换的能力,允许在调试器会话过程中更改源代码,要使用该功能,只需在编辑器中更改代码并恢复调试。所以,只要在eclipse等IDE里改了源代码并编译,马上就可以应用到运行中环境。在一定程度上,完全可以用eclipse等IDE的debug调试功能来代替应用服务器的hot deploy热部署。当然在某种情况下,hot deploy热部署还是需要的。

下面介绍配置Tomcat和Eclipse集成调试配置:

How do I configure Tomcat to support remote debugging?

The short answer is to add the following options when the JVM is started:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
There are a number of ways you can do this depending on how you normally start Tomcat:

  • Set environment variables JPDA_ADDRESS=8000 and JPDA_TRANSPORT=dt_socket and then start tomcat using catalina jpda start(sh catalina.sh jpda start).
  • If you run Tomcat using service wrapper, check the documentation for the service to determine how to set the required JVM options.
  • If you start Tomcat from within an IDE, check the documentation for the IDE to determine how to set the required JVM options.

 

Using catalina start and CATALINA_OPTS

Alternatively, you can set the java program's command-line arguments for the JPDA settings. For Tomcat, you specify them in the CATALINA_OPTS environment variable and the catalina.sh or catalina.bat script adds the value of the environment variable to the java command that starts Tomcat; for example:

bash:

declare -x CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"
$JWSDP_HOME/bin/catalina.sh start

csh:

setenv CATALINA_OPTS "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"
$JWSDP_HOME/bin/catalina.sh start

Windows:

set JPDA_TRANSPORT=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
%JWSDP_HOME%\bin\catalina start
The port does not need to be set to 8000, it may be any value appropriate for your system.

Whilst this is very useful in development it should not be used in production because of both security and performance implications.


 How do I remotely debug Tomcat using Eclipse?

This answer assumes that you have a project set up with all of the fixings and have some idea of what you're doing in this respect. If not then thats really outside the scope of this topic and more in the scope of you needing to go to eclipse.org and read up on how to use your ide, and maybe practice a little bit before you come back to this. We're also going to assume you have some idea of what a debugger is and how to use one.

Make sure tomcat is started and that your app is deployed and the sources, etc are all defined as resources in your app. If you have a servlet or something, set a breakpoint where its sure to hit on the next request. Go to "Run->Debug...". Click on "Remote Java Applications", then click "New". Type in the title and all. Notice that port 8000 from the Tomcat instructions. Save and run. Eclipse will connect to the VM that Tomcat is running under. Wow, that was easy! Now go type the url to submit to your servlet or whatever in your browser. Boom you hit the breakpoint right? Have fun!

你可能感兴趣的:(java,eclipse,programming)