65

How to enable remote debugging from IntelliJ with Tomcat?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user729344
  • 849
  • 2
  • 9
  • 8

9 Answers9

53

Use Tomcat Run/Debug Configuration. Click the enter image description here button and choose new Remote configuration. In the Configuration tab copy the JVM options suggested by IDEA.

Run Tomcat with the suggested JVM options:

set JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=1317,suspend=n,server=y
startup

Press Debug button in IDEA.

Otherwise it's the same as for Local Tomcat, check the tutorial.

Note that deploying to Tomcat is available only in IDEA Ultimate, but you can still manually deploy to Tomcat on your own and use Java's remote debug features using the free edition.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Important: the address parameter needs to be some high port to avoid issues on Linux hosts. Only when I tried this with 25239 or something like that, it worked :) – Alfabravo Jan 17 '18 at 19:45
  • You could also set up maven deployment to tomcat. Add to your pom.xml following ` org.apache.tomcat.maven tomcat7-maven-plugin 2.2 tomcat s3cret http://localhost:8081/manager localhost /portal `. Then from Intellij run maven `tomcat7:deploy`. – Developer Marius Žilėnas Mar 15 '18 at 06:31
  • How to examine and configure Tomcat JVM options on Windows: https://stackoverflow.com/questions/5984540/how-do-i-increase-memory-on-tomcat-7-when-running-as-a-windows-service – Vadzim Jan 22 '19 at 13:15
  • Your "check the tutorial" reference leads to no tutorial, but a start page of the JI enterprise. – Gangnus Sep 07 '22 at 08:55
  • @Gangnus I've updated the tutorial link to a third-party resource which describes the remote debug process for Tomcat step by step and looks accurate to me. The original IntelliJ IDEA tutorial that was linked no longer exists, sorry. – CrazyCoder Sep 07 '22 at 16:25
  • The smart tomcat tutorial is for the old UI. Is there a guide for the new UI anywhere? – Kentaro T. Vadney Aug 16 '23 at 20:00
51

Assume you start Tomcat standalone and attach debugger remotely to it from IntelliJ IDE.

1) Start Tomcat in debug mode
   $TOMCAT_HOME/bin/catalina.sh jpda start

   By default JPDA_ADDRESS is defined as "localhost:8000" in catalina.sh
   Change to a different port as need

2) In IntelliJ IDE
   Click Run > Edit Configurations
   Click + icon on the top-left toolbar
   Click Remote
   Enter a name you want in Name input box
   Enter 8000 in Port input box under Settings section

3) Click Apply, then OK
4) Run > Debug..., Click the configuration you just created

Note:

1) This should work with other remote JPDA instances such as WebLogic, JBoss, etc.
2) Documentation environment: Linux, IntelliJ IDEA 15.0.2

IntelliJ Remote Debugger Configuration

Jonathan L
  • 9,552
  • 4
  • 49
  • 38
  • Error occurred during initialization of VM... ERROR: Cannot load this JVM TI agent twice, check your java ...command line for duplicate jdwp options... agent library failed to init: jdwp... Disconnected from server... – Gangnus Aug 11 '17 at 11:35
  • 1
    I am afraid these recomendations are for nothing - they are for some very old IntelliJ versions. – Gangnus Aug 11 '17 at 11:36
14

just start catalina using following command:

catalina jpda start

By default tomcat will start and listen on port 8000.

See also http://wiki.apache.org/tomcat/FAQ/Developing#Q1

Zaur_M
  • 737
  • 3
  • 11
  • 18
  • 3
    note - if you use 'catalina jpda run', remote debugging will work and Tomcat will run in your console, which is sometimes helpful. – Ian Durkan Jul 24 '13 at 16:28
  • export JPDA_ADDRESS=0.0.0.0:8000 sh catalina.sh jpda start 0.0.0.0 allows from all the interfaces. You can also configure to one IP. – User007 Sep 25 '19 at 07:35
11

TOMCAT Configuration Instructions

The process of getting remote debugging working involves two steps.

 1. Starting Tomcat with remote debugging enabled
 2. Having your IDE, in my case IntelliJ IDEA, to be able to debug the remote tomcat application.

There are couple of ways to get the first part done and it slightly differs depending on which OS environment your Tomcat instance is running on. But, regardless of the method used, the main idea behind the configuration remains the same; which is: pass specific start up options to the JVM that would enable remote debugging.

If you have Tomcat running as a windows service, then configuring Tomcat to start up with ability to be debugged remotely is done by simply specifying the start up arguments in the run properties.

Open up the Apache Tomcat properties dialog box :

Apache Tomcat/bin/tomcat9w.exe

and under the Java tab add the required start up option:

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

enter image description here

Restart your server now.    
Close and go to your IDE.

Configuring IntelliJ IDEA

With the remote JVM running the Tomcat started with the required start up arguments, the next thing to do is to configure the debugger in IntelliJ IDEA.

Open the Edit Configuration settings and select the Remote option:

The Remote settings dialog box appears where you can specify the required configuration; remote host, port, project, etc...

enter image description here

Specify the required settings, click Ok to save changes, and start the debugging session. You should also see the notice that IntelliJ has successfully connected to the remote VM.

Once this is done, you should then open the source code of the application you have running on the remote Tomcat, put a breakpoint where required and you can go ahead and start debugging as if the application is running on your local machine.

François
  • 509
  • 5
  • 7
7

Remote debugging of web applications running in Tomcat 7 is extremly convenient in IntelliJ IDEA 12 if you use Maven!

Just configure your pom-file to use the tomcat7-maven-plugin, eg:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
</plugin>

Then use the "Maven Projects" tool window to start the tomcat7:run goal in debug mode (see screenshot).

enter image description here

Hachmaninow
  • 572
  • 5
  • 6
5

In Intellij Idea:

  1. Click on Run -> Edit Configurations -> Add New Configurations ("+" icon)

  2. Click on Remote

  3. Set Host and Port

  4. Copy content of Command line arguments for running remote JVM, for example:

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

enter image description here

If you use Tomcat Service (for Tomcat 9):

  1. Go to %TOMCAT_INSTALL_DIR%/bin

  2. Run tomcat9w.exe

  3. Click on Java tab

  4. In Java Options Paste the copied text:

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

  5. Apply.

enter image description here

Community
  • 1
  • 1
Taras Melnyk
  • 3,057
  • 3
  • 38
  • 34
4

Add these two line to your bin/setenv.sh

export JPDA_ADDRESS="5005"
export JPDA_TRANSPORT="dt_socket"

Add remote Run/Debug Configuration in IntelliJ IDEA. (see the answer from @Jonathan L). You can keep the default port '5005'.

Start tomcat,

catalina.sh jpda run

Then simply run debug on the IDE.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
3

you can set

export CATALINA_OPTS="$CATALINA_OPTS -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

in setenv.sh file in bin tomcat directory (remote server) and restart it.

then just you should add Remote in Run/Debug Configurations in intellij and set Host to remote server and Port 8000

Alireza Alallah
  • 2,486
  • 29
  • 35
3

Step1 (How to start tomcat):

cd /C/SOFTWARE/apache-tomcat-8.5.38/bin 
JAVA_OPTS='-agentlib:jdwp=transport=dt_socket,address=54470,suspend=n,server=y' sh catalina.sh start
ps aux | grep java

Step 2 ( Attach Intellij with remote port ):

Run/Debug Configurations
    Tab: Server:
       Application Server: Type "Tomcat 8.5.38"
       Open browser -> URL -> http://localhost:8080/
       Tomcat Server Settings -> Type -> Same File System
                              -> Host -> Same File System

       Remote Connection Settings
                           -> Host -> localhost
                           -> Port -> 8080

Startup/Connection Tab
       Run -> No specific parameters needed.
       Debug -> -agentlib:jdwp=transport=dt_socket,address=54470,suspend=n,server=y
user204069
  • 1,215
  • 3
  • 19
  • 25