使用 Eclipse 远程调试 Tomcat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3835612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:39:08  来源:igfitidea点击:

Remote debugging Tomcat with Eclipse

eclipsetomcat

提问by victor

I can't seem to debug the tomcat application through Eclipse. I've set

我似乎无法通过 Eclipse 调试 tomcat 应用程序。我已经设置了

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

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

and then I run bin/catalina.sh, where I see output saying it's listening for dt_socketon port 8000. But whenever I try to connect to port 8000in Eclipse (by adding an entry in the remote java application menu), it complains about a refused connect. Any ideas?

然后我运行bin/catalina.sh,在那里我看到输出说它正在监听dt_socketon port 8000。但是每当我尝试连接到8000Eclipse 中的端口时(通过在远程 Java 应用程序菜单中添加一个条目),它都会抱怨连接被拒绝。有任何想法吗?

回答by Raghuram

Can you check if this works?

你能检查一下这是否有效吗?

JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

catalina.sh jpda start

回答by Ravi MCA

I spent some time on this to get the right information.

我花了一些时间来获取正确的信息。

So here is the detailed information step by step.

所以这里是一步一步的详细信息。

Environment : Windows 7

环境:Windows 7

TomCat version : 7.0

汤姆猫版本:7.0

IDE : Eclipse

IDE : 日食

Configurations to be added for enabling remote debugging with in tomcat is

为在 tomcat 中启用远程调试而添加的配置是

-Xdebug
-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n

I don't recommend above configuration fro non windows environment. To add the above configuration double click on tomcat server which will be available in server view. Find the below screen shot. enter image description here

我不推荐非 Windows 环境的上述配置。要添加上述配置,双击将在服务器视图中可用的 tomcat 服务器。找到下面的屏幕截图。 在此处输入图片说明

Now add the above runtime environment configuration to tomcat. For this check below screenshot.

现在将上面的运行环境配置添加到tomcat中。对于这个检查下面的截图。

enter image description here

在此处输入图片说明

Now got to Arugments tab in Edit launch configuration properties as show in below screen shot.

现在进入编辑启动配置属性中的 Arugments 选项卡,如下面的屏幕截图所示。

enter image description here

在此处输入图片说明

GoTo VM arguments section add these lines.

GoTo VM arguments 部分添加这些行。

-Xdebug

-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n

-Xdebug

-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n

enter image description here

在此处输入图片说明

Now got to debug button available on eclipse toolbar.

现在可以使用 Eclipse 工具栏上的调试按钮。

enter image description here

在此处输入图片说明

In Debug configurations find "Remote Java Application" and double click on it.enter image description here

在调试配置中找到“远程 Java 应用程序”并双击它。在此处输入图片说明

In Name field enter any name which you like to.

在名称字段中输入您喜欢的任何名称。

From project field using browse button select the project which you want to perform remote debug.

从项目字段中使用浏览按钮选择要执行远程调试的项目。

The hostname is nothing but the host address. Here i'm working locally so it is "localhost".

主机名就是主机地址。在这里,我在本地工作,所以它是“本地主机”。

Last the Port column the value should be 8000. Apart from Name and Project text fields other two columns Host and port will be filled by eclipse itself if not make you have same values as mentioned. Check Screen shot for info.enter image description here

最后端口列的值应该是 8000。除了名称和项目文本字段之外,其他两列主机和端口将由 eclipse 本身填充,如果不是使您具有与提到的相同的值。检查屏幕截图以获取信息。在此处输入图片说明

Now right click on TomcatServer in server console select Add and Remove from context menu. From this dialog you can add the project to server.

现在右键单击服务器控制台中的 TomcatServer,从上下文菜单中选择添加和删除。从此对话框中,您可以将项目添加到服务器。

Now run the Tomcat sever.

现在运行Tomcat服务器。

enter image description here

在此处输入图片说明

Now run the TomCatDebugConfiguration from Debug Tool.

现在从调试工具运行 TomCatDebugConfiguration。

Last open internal or external browser and run your project. If the execution control reached the break points then the eclipse will prompt for debug perspective.

最后打开内部或外部浏览器并运行您的项目。如果执行控制到达断点,则 eclipse 将提示调试透视图。

回答by karl

In the tomcat bin directory wherecatalina.bator .shis found (aka {CATALINA_BASE}/bin), edit (create if not there):

在找到catalina.bator的 tomcat bin 目录.sh(又名 {CATALINA_BASE}/bin)中,编辑(如果不存在则创建):

setenv.bat/.sh

Add the following line:

添加以下行:

CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

That's all you need to do, you don't need to edit the catalina.bat(or .sh) file.

这就是您需要做的所有事情,您不需要编辑catalina.bat(或.sh)文件。

See the comments in catalina.bator catalina.sh.

请参阅catalina.bat或 中的注释catalina.sh

You may have to adjust the syntax for your particular environment/situation. For example, if you already have CATALINA_OPTS defined, you might do something like this (in a windows environment):

您可能需要针对您的特定环境/情况调整语法。例如,如果您已经定义了 CATALINA_OPTS,您可以执行以下操作(在 Windows 环境中):

set CATALINA_OPTS=%CATALINA_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,address=8088,server=y,suspend=n

To debug from Eclipse:

从 Eclipse 调试:

run->Debug configurations...->Remote Java Application->New

运行->调试配置...->远程Java应用程序->新建

and specify a name, the project you are debugging, and the tomcat host and debug port specified above.

并指定名称、您正在调试的项目以及上面指定的 tomcat 主机和调试端口。

回答by RAJ

In catalina.batfile please modify the below.

catalina.bat文件中,请修改以下内容。

  • Step 1: CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

  • Step 2: JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

  • Step 3: Run Tomcat from command prompt like below: catalina.sh jpda start

  • Step 4: Then in the eclipse create a debug configuration

    1. Give any name for configuration.
    2. Give the project name.
    3. Give the connection type as Standard(Socket Attach)
    4. host as localhost
    5. port as 8000( or any port number , but that should be same in other places also).
  • 第1步: CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

  • 第2步: JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

  • 第 3 步:从命令提示符运行 Tomcat,如下所示: catalina.sh jpda start

  • 第四步:然后在eclipse中创建一个调试配置

    1. 为配置提供任何名称。
    2. project name.
    3. 将连接类型指定为 Standard(Socket Attach)
    4. 主机为 localhost
    5. 端口为8000(或任何端口号,但在其他地方也应该相同)。

回答by DAB

Many of the above answers are correct, but remember that by default the debugger will listen on localhost only. If you are debugging a remote serveryou will need to specify the ip address to listen on, for example

上面的许多答案都是正确的,但请记住,默认情况下,调试器只会在 localhost 上侦听。例如,如果您正在调试远程服务器,则需要指定要侦听的 IP 地址

JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=10.1.1.33:8000,server=y,suspend=n"

catalina.sh jpda start

JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address= 10.1.1.33:8000,server=y,suspend=n"

catalina.sh jpda 开始

Note that the address is now 10.1.1.33:8000

注意地址现在是10.1.1.33:8000

Of course, you can also check which IP is actually being used, by running

当然,您也可以通过运行来检查实际使用的是哪个 IP

netstat -an

netstat -an

on both windows and Linux and filter out the port with find (Win) or grep (Linux).

在 Windows 和 Linux 上,并使用 find (Win) 或 grep (Linux) 过滤掉端口。

回答by Mayen

Let me share the simple way to enable the remote debugging mode in tomcat7 with eclipse (Windows).

分享一下在tomcat7中用eclipse(windows)开启远程调试模式的简单方法。

Step 1: open bin/startup.bat file
Step 2: add the below lines for debugging with JDPA option (it should starting line of the file )

Step 1: open bin/startup.bat file
Step 2: 添加下面几行用 JDPA 选项调试(它应该是文件的起始行)

    set JPDA_ADDRESS=8000  
    set JPDA_TRANSPORT=dt_socket  

Step 3: in the same file .. go to end of the file modify this line -

第 3 步:在同一个文件中 .. 转到文件末尾修改这一行 -

    call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%  
    instead of line  
    call "%EXECUTABLE%" start %CMD_LINE_ARGS%  

step 4: then just run bin>startup.bat (so now your tomcat server ran in remote mode with port 8000).

第 4 步:然后只需运行 bin>startup.bat(所以现在您的 tomcat 服务器以端口 8000 的远程模式运行)。

step 5: after that lets connect your source project by eclipse IDE with remote client.

第 5 步:之后让我们通过 Eclipse IDE 将您的源项目与远程客户端连接起来。

step6: In the Eclipse IDE go to "debug Configuration"

步骤 6:在 Eclipse IDE 中转到“调试配置”

step7:click "remote java application" and on that click "New"

步骤7:单击“远程java应用程序”,然后单击“新建”

step8. in the "connect" tab set the parameter value

步骤 8. 在“连接”选项卡中设置参数值

   project= your source project  
   connection Type: standard (socket attached)   
   host: localhost  
   port:8000  

step9: click apply and debug.

step9:点击apply and debug。

so finally your eclipse remote client is connected with the running tomcat server (debug mode).

所以最后你的 eclipse 远程客户端连接到正在运行的 tomcat 服务器(调试模式)。

Hope this approach might be help you.

希望这个方法可以帮到你。

Regards..

问候..

回答by atamanroman

Just run ./catalina.sh jpda start(forks) or ./catalina.sh jpda run(does not fork, not mentioned in help). All options mentioned here default to sane values.

只需运行./catalina.sh jpda start(分叉)或./catalina.sh jpda run(不分叉,帮助中未提及)。这里提到的所有选项都默认为合理的值。

回答by shermi

Modify catalina.bat to add

修改 catalina.bat 添加

set JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n" 

and

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

Optional: Add below line to run the debug mode by default when you run startup.bat

可选:添加以下行以在运行 startup.bat 时默认运行调试模式

call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%

Eclipse or STS select debug configuration right click -> new

Eclipse 或 STS 选择调试配置右键单击 -> 新建

connection type -> Standard socket Attach
Port -> 8000 (as given in the CATALINA_OPTS)
Host -> localhost or IP address

回答by Marcelo Salazar

In tomcat 7, catalina.sh has this code:

在 tomcat 7 中,catalina.sh 具有以下代码:

if [ "" = "jpda" ] ; then
  if [ -z "$JPDA_TRANSPORT" ]; then
    JPDA_TRANSPORT="dt_socket"
  fi
  if [ -z "$JPDA_ADDRESS" ]; then
    JPDA_ADDRESS="8000"
  fi
  if [ -z "$JPDA_SUSPEND" ]; then
    JPDA_SUSPEND="n"
  fi
  if [ -z "$JPDA_OPTS" ]; then
    JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
  fi
  CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS"
  shift
fi

Ii implies that you can setup JPDA with:

Ii 意味着您可以通过以下方式设置 JPDA:

export JPDA_TRANSPORT=dt_socket
export JPDA_ADDRESS=8000
export JPDA_SUSPEND=n

Or with:

或与:

JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

And finally use:

最后使用:

catalina.sh jpda start

catalina.sh jpda start

Regards

问候

回答by shachar

If still all the above doen't work you can always add to the script

如果以上所有方法仍然无效,您可以随时添加到脚本中

    set "JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"