使用Eclipse远程调试发布在Tomcat上的Web应用

46 篇文章 0 订阅
Remote Debugging with Eclipse说明了远程调试的原理,说明了让java程序支持被远程调用所需要的java参数如下
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044

Remote Debugging with Eclipse

At 8:22 PM on Nov 1, 2005, Levent Gurses  wrote:
<!-- Synopsis -->

How many times trying to fix a server-side Java problem appeared trivial, but getting to the source of the problem took all the time? A remote debugger attached to a Java application can shorten the defect-discovery times significantly and make the process more enjoyable.

<!-- What is it -->

The Java Debugger

The Java Debugger (jdb) is a dynamic, controlled, assignment-based debugging tool. It helps find and fix bugs in the Java language programs both locally and on the server. To use jdb in a J2EE application server you must first launch it with debugging enabled and attach to the server from the debugger through a JPDA port (Default port is 1044).

The default JPDA options for J2EE servers are as follows:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044

The jdb parameters specify the way debugger will operate. For instance transport=dt_socket instructs the JVM that the debugger connections will be made through a socket while the address=1044 parameter informs it that the port number will be 1044. Similarly, if you substitute suspend=y , the JVM starts in suspended mode and stays suspended until a debugger is attached to it. This may be helpful if you want to start debugging as soon as the JVM starts.

 


Debugging WebLogic

Debugging WebLogic is no different than debugging any other Java remote application. You need to make sure to launch it with the required debugging arguments and attach a debugger. In the case of WebLogic 8.1, you need to add these arguments to the startup script. WebLogic comes with several launch scripts (*.sh and *.cmd) under BEA_HOME/weblogic81/server/bin.

  1. Locate startWSL.cmd and add the following variable DEBUG_OPTS:
    set DEBUG_OPTS = -Xdebug -Xrunjdwp:transport= dt_socket,address=1044,server=y,suspend=n
    
  2. Next, insert the new variable to the WebLogic startup command, after "%JAVA_HOME%/bin/java" and preferably before the other options.
  3. Your startup script should look like:

    "%JAVA_HOME%/bin/java" %DEBUG_OPTS% %JAVA_VM% %MEM_ARGS% %JAVA_OPTIONS%-Dweblogic.Name=%SERVER_NAME% -Dweblogic.management.username= %WLS_USER%-Dweblogic.management.password= %WLS_PW% -Dweblogic.management.server= %ADMIN_URL%-Dweblogic.ProductionModeEnabled= %PRODUCTION_MODE%-Djava.security.policy= "%WL_HOME%/server/lib/weblogic.policy" weblogic.Server

Debugging JBoss

Same as WebLogic, except that you need to change run.bat/run.sh located under JBOSS_HOME/bin.

Linux users should see something similar to this:

 $ cd /var/jboss4/bin
 $ sh ./run.sh 
 =========================================================================

 JBoss Bootstrap Environment
 
 JBOSS_HOME: /var/jboss4
 
 JAVA: /usr/java/j2sdk1.4.2_06/bin/java
 
 JAVA_OPTS: -server -Xms128m -Xmx128m -Dprogram.name=run.sh
 
 DEBUG_OPTS = -Xdebug -Xrunjdwp:transport= dt_socket,address=1044,server=y,suspend=n
 
 CLASSPATH: /var/jboss4/bin/run.jar:/usr/java/j2sdk1.4.2_06/lib/tools.jar
 
 =========================================================================

Debugging Tomcat

Again, very much similar to WebLogic and JBoss, except that you need to change catalina.bat/catalina.sh located under TOMCAT_HOME/bin.


Debugger Verification

Now you can launch your application in debug mode. Just to make sure that the server is listening to port 1044 you can run netstat /a. You should see port 1044 in the list of open ports (See Figure 1: List of open ports: netstat -a).

Figure 1 List of open ports: netstat -a

 


The Eclipse Connection

After making sure WebLogic is listening for incoming connections on port 1044, what is left is to tell Eclipse to connect to this port and you are ready to debug.

  1. In Eclipse, navigate to Run | Debug (See Figure 2: Create new Remote Java Application configuration in Eclipse ).
  2. Select Remote Java Application , on the left column. Click New , on the bottom of the same column.
  3. In the Create configuration screen you'll be prompted to enter some values. Start with a meaningful name. In my case that's WebLogic Instance . For Project, select the Java project that contains the source code you want to debug. Leave Connection Type in default, i.e. Standard (Socket Attach) . For Host , enter localhost. If you want to debug a remote server, enter its hostname or IP address. For port, enter 1044 or the port you defined in your WebLogic startup script.
  4. Click Apply
  5. Make sure WebLogic instance is running in debug mode. In the same screen click Debug . Eclipse should automatically take you to the Debug perspective and you should see a stack trace in the Debug view.
  6. If you are not automatically taken to the Debug perspective, select Window | Open Perspective | Other and then click Debug.

Figure 2 Create new Remote Java Application configuration in Eclipse
<span http://

Eclipse Debug window should automatically pop-up with the stack pointer on your first breakpoint (See Figure 3: Breakpoint hit in Eclipse's debugger ). After that, you can use all the various functions that the debugger has to offer, namely variable assignments, step-into, drop to frame, etc.

 

 

 


Figure 3 Breakpoint hit in Eclipse debugger


References


System Information

  • Windows 2000
  • JDK 1.4.2_03
  • Eclipse 3.0
  • BEA WebLogic 8.1
  • JBoss 4.0.2
  • Tomcat 5.0.26

以及如何配置远程调试weblogic、jboss和tomcat,但是里面对如何让tomcat支持远程调用并没有仔细说。再搜索,在tomcat的FAQ就找到了:
How do I configure Tomcat to support remote debugging?
如上面所示,其中的关键在于如何正确的启动tomcat。对于非windows平台下的操作来说,需要把%TOMCAT_HOME%/bin/startup.sh中的最后一行exec "$PRGDIR"/"$EXECUTABLE" start "$@" 中的start改成jpda start。如果的8000端口有其他用处的话,那么还需要修改catalina.sh文件,看其中的说明,添加一行JPDA_ADDRESS=”1044”或者其他你指定的端口。这样就可以通过startup.sh或者catalina.sh jpda start来其中支持远程调试的tomcat了。
在windows平台上是一样的步骤,只不过.sh文件改成了.bat文件了。然后需要注意以下这些地方,
1、 catalina.bat文件默认的JPDA_TRANSPORT是dt_shmem,但是Eclipse只支持dt_socket,所以需要在catalina.bat中添加一行 set JPDA_TRANSPORT=”dt_socket”
2、 catalina.bat文件默认的端口是jdbconn,我也不知道这个端口是多少,因此添加一个 set JPDA_ADDRESS="1044"

 

About

This section of the FAQ discusses common questions related to Tomcat development.

 

Questions

  1. How do I configure Tomcat to support remote debugging?

  2. How do I remotely debug Tomcat using Eclipse?

  3. How do I remotely debug Tomcat using NetBeans?

  4. How do I change the monitoring interval for modified resources and application reloading?

 

Answers

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:

  • If you run Tomcat using service wrapper, add the above JVM options before any other JVM options. Check the documentation for the service to determine how to set JVM options.
  • Set environment variables JPDA_ADDRESS=8000 and JPDA_TRANSPORT=dt_socket and then start tomcat using catalina jpda start.

  • If you start Tomcat from within an IDE, check the documentation for the IDE to determine how to set the required JVM options.

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 Configurations...". 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!

How do I remotely debug Tomcat using NetBeans?

This answer assumes that you have correctly set up a NetBeans project and that you know how to use the NetBeans debugger. If not, please go to http://www.netbeans.org/kb/using-netbeans/40/debug.html and read up on how to use NetBeans and its debugger.

Make sure that Tomcat is started in debug mode as described above, that your application is deployed, and that the sources are all defined as resources in your application. If you have a servlet or JSP file, set a breakpoint where you think a problem might be occurring. Go to "Run->Attach Debugger". A dialog pops up to let you specify the following options:

  • Debugger: JPDA Debugger
  • Connector: SocketAttach

  • Host: The IP address of the host your Tomcat installation is running on (127.0.0.1 if it is your local machine).
  • Port: The port of your Tomcat debugging interface, which should be 8000 if you've followed the instructions above.

When you press OK, you have a debugging connection very similar to local debugging.

Note that NetBeans has a second option -- you can debug JSP files and servlets locally using a Tomcat server that is bundled with the IDE. When you debug a JSP file or servlet in the IDE, the bundled Tomcat server automatically starts in debug mode, and the debugger connects to it.

How do I change the monitoring interval for modified resources and application reloading?

Modify the checkInterval attribute value on the relevant Loader element in your web application configuration file (yourapp.xml), or in the main configuration file %CATALINA_HOME%/conf/server.xml if that is the one you're using. For more information, please see the Loader configuration reference].

 

 

另可参见: http://java.chinaitlab.com/Eclipse/36364.html

 另可参见:http://hi.baidu.com/plflying/blog/item/4f83ce021fec89054bfb51a4.html

 

 

博主:以前按上面介绍及tomcat文档配置JPDA_OPTS="-Xdebug -Xrunjdwp:transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"即可

最近不知怎么回事不能成功必须加上

 CATALINA_OPTS="-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=$JPDA_TRANSPORT,server=y,suspend=n,address=$JPDA_ADDRESS"

综上 eclipse+tomcat 远程调试配置(至少适用于eclipse3.3+tomcat 6.X):

修改 tomcat/bin/catalina.sh,在“# ----- Execute The Requested Command ---” 前添加

CATALINA_OPTS="-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787"          【Linux环境】

SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787        【Windows环境】

其中8787为端口号

打开eclipse中的debug设置窗口,选择Remote Java Application ,新建一个debug项,输入服务器IP(如果是本机就输入localhost或127.0.0.1)和刚才设置端口号,点ok就可以进入debug状态了

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值