18

I have a breakpoint on a line where is the System.out.println("test") command. I believe that the command is reached by execution because I see the printed string "test". But the breakpoint is ignored.

Breakpoint is a red circle all the time, without a tick or cross. I think this is an issue when IDEA thinks the class is not loaded, while it is, because the command is executed.

I can reproduce it in various circumstances:

  1. When I press debug (with maven configuration install exec:exec -DforkMode=never)

  2. Remote debugging - I run maven goal in debug mode in the console:

    mvnDebug install exec:exec -DforkMode=never

    or

    mvnDebug install exec:exec

    remote debug configuration in IDEA:

    • Arguments for running remote JVM:
      -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
    • For JDK 1.4.X:
      -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
    • Transport: Socket
    • Debugger mode: Attach
    • Host: localhost
    • Port: 8000

In both cases the debugger only prints "Connected to the target VM, address: 'localhost:8000', transport: 'socket'"

I have also tried File > Invalidate Caches / Restart and clean build, but the breakpoint is still ignored.

Configuration:

Ubuntu 13.10
IntelliJ IDEA Ultimate build 133.944
Apache Maven 3.0.4
Java version: 1.7.0_51, vendor: Oracle Corporation
OS name: "linux", version: "3.11.0-17-generic", arch: "amd64", family: "unix"

EDIT: relevant part of pom.xml:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <configuration>
    <executable>java</executable>
      <arguments>
        <argument>-D--secret--.server.configuration=/usr/local/etc</argument>
        <argument>-classpath</argument><classpath/>
        <argument>com.--secret--.Server</argument>
      </arguments>
  </configuration>
</plugin>
mirelon
  • 4,896
  • 6
  • 40
  • 70
  • This could be that the classes that you have in "target" were compiled without debug information. Are you building your application using IntelliJ or using maven? I would delete the target directory, and then try to debug using the debug button in IntelliJ. Also do you have some special build step in in your pom.xml ? AspectJ or similiar? – Andres Olarte Mar 06 '14 at 16:29
  • We use maven for building. When I deleted the target directory and pressed "debug", I noticed maven-compiler-plugin compiles the classes into `target/`. But still is the breakpoint ignored. Build steps in pom.xml are here: http://pastebin.com/zSHh5kTf – mirelon Mar 06 '14 at 16:59
  • I don't see anything in your pom that might interfere. What kind of application is this ? A webapp? A standalone app with a main method? When you mention (with maven configuration install exec:exec -DforkMode=never), where are you setting that? – Andres Olarte Mar 06 '14 at 18:56
  • It is basically a server application that is listening on a port for xml requests - that's when the breakpoints are ignored. On the other hand, when I run unit tests, the breakpoints just work. The maven configuration is the maven goal set in the IDE (upper right dropdown - "Edit configurations") – mirelon Mar 06 '14 at 19:49

3 Answers3

8

Update 2021:

Nowadays, on most situations, debugging should work out of the box.

Newer versions of IntelliJ IDEA (tested with 2020.3) can now auto-detect maven exec configurations and add the proper options to enable debugging. See IDEA-189973 for further info. Thanks @Gili for opening a ticket for this functionality back in 2018.

Nevertheless my original answer bellow can still be useful for older versions of IntelliJ, Remote Debugging or to debug while using certain Maven / Gradle plugins that Fork the VM and require debugging options to be manually passed downstream (adjust configuration accordingly).


My solution:

Considering that you have a program that depends on system properties:

package com.mycompany.app;


public class App {

    private static final String GREETING = System.getProperty("greeting", "Hi");

    public static void main(String[] args) {
        int x = 10;
        System.out.println(GREETING);
    }
}

And you are running it with exec:exec:

mvn exec:exec -Dexec.executable=java "-Dexec.args=-classpath %classpath -Dgreeting=\"Hello\" com.mycompany.app.App"

With some "inception magic" we can debug the process started by Mavenexec:exec.

Maven

Change your exec:exec goal to enable remote debugging. I'm using suspend=y and server=n, but feel free to configure the JDWP Agent as you please:

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

This will not be passed directly to the maven JVM, instead it will be passed to exec.args which will be used by exec:exec:

mvn exec:exec -Dexec.executable=java "-Dexec.args=-classpath %classpath -agentlib:jdwp=transport=dt_socket,server=n,address=127.0.0.1:8000,suspend=y -Dgreeting=\"Hello\" com.mycompany.app.App"

IntelliJ IDEA

Create a Remote configuration (again I'm using a Listen strategy. You should adjust it accordingly):

enter image description here

Now toggle your breakpoints and Debug your remote configuration. Using the settings above it will wait until your process starts:

enter image description here

Finally run the exec:exec line above and debug your application at will:

enter image description here


So basically you need two "Run/Debug" configurations for this to work:

  1. A Maven configuration for exec:exec with the system properties and JDWP agent configuration:

enter image description here

  1. The remote configuration acting as a client.
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • Actually, this two-step solution worked. Although, we have the VM arguments already stored in pom.xml, so adding that parameter `-agentlib:jdwp=transport=dt_socket,address=localhost:8000,server=y,suspend=n` sufficed. – mirelon Mar 13 '14 at 10:16
  • 1
    I wonder if IDEA plans to support debugging maven projects (in Eclipse/Netbeans it just works when you press the Debug button) – mirelon Mar 13 '14 at 10:17
  • Mirelon, IDEA does support debugging Maven projects out of the box. Normally you just need to click on the debug button (`Shift+F9`). The problem here is your call to `exec:exec` that spawns a different process. This setting requires remote debugging [even on Eclipse and Netbeans](http://stackoverflow.com/questions/2935375/debugging-in-maven). – Anthony Accioly Mar 13 '14 at 14:34
  • I've just tested this settings with Eclipse Kepler (Custom maven `Debug Configuration` with `exec:exec`) and Netbeans 7.4 (Custom Maven action with `exec:exec`, with or without `jpda.listen=maven`). Both skipped the breakpoints when running directly. Netbeans required me to use `Attach Debugger...` and Eclipse required me to launch a separate `Remote Java Application` debug configuration. – Anthony Accioly Mar 13 '14 at 15:07
  • Netbeans automatically overrides exec.args when you run your project debug mode. No need for separate configurations. This is true for a while now, but definitely in version 8.2 – Gili Apr 10 '18 at 14:47
  • Yeah, I think that all IDEs mentioned here are a little bit smarter nowadays. My answer is 4 years old and doesn't necessary apply to newer versions. Would be good to review IDEA 2018.1 / Netbeans 8.2 / Eclipse Oxygen. – Anthony Accioly Apr 10 '18 at 15:09
  • 2
    IDEA still doesn't support it (as of 2018.1). I filed this RFE: https://youtrack.jetbrains.com/issue/IDEA-189973 – Gili Apr 10 '18 at 15:57
1

The exec goal will execute your program in a separate process, so the debugger may not be connecting to the right JVM. Instead try using the java goal, e.g.:

mvnDebug install exec:java 

This will execute your program in the same process and hopefully you will hit your breakpoint.

Jonathan
  • 20,053
  • 6
  • 63
  • 70
  • In Run/Debug Configurations there are only these categories: 1. Maven, 2. Remote, 3. TestNG, 4. Tomcat Server, 5. Defaults. We currently have all the defined configurations in the first category (and I also tried the Remote). Where exactly do you mean we should add the java goal? – mirelon Mar 10 '14 at 09:25
  • In the Maven Run/Debug Configuration, you can put the Maven goals you want to execute in the `Command Line` text box under the `Parameters` tab. – Jonathan Mar 10 '14 at 09:56
  • I get it. I need to add additional arguments that have been specified in pom.xml. I edited the question and added a relevant part. How should I modify pom.xml? Now there is "Unable to parse configuration of mojo org.codehaus.mojo:exec-maven-plugin:1.2.1:java: Cannot assign configuration values to array of type java.lang.String: [-Ddante.server.configuration=/usr/local/etc, -classpath, Classpath {}, com.digmia.dante.server.impl.Server]" – mirelon Mar 10 '14 at 10:38
  • I would have thought that if you run `exec:exec` or `exec:java` from within IntelliJ, IntelliJ should use the plugin configuration in your `pom.xml`. – Jonathan Mar 10 '14 at 15:18
  • Yeah, that's true. But when I run `exec:java`, it cannot parse it correctly. – mirelon Mar 11 '14 at 08:39
  • I don't think you can use `exec:java` with VM Arguments (check [this](http://stackoverflow.com/questions/7305090/how-to-run-a-java-project-with-vm-arguments-from-command-line) out). – Anthony Accioly Mar 12 '14 at 22:05
1

To debug web applications in maven projects using the Intellij Community Edition, you can add a tomcat or jetty plugin to your WAR pom like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8080</port>
                <path>/yourapp</path>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
        </plugin>
    </plugins>
</build>

It's possible if needed to add database drivers like this:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <dependencies>
        <dependency>
           ... your database driver groupId and artifactId ...
        </dependency>
    </dependencies>
</plugin>

Then using these plugins the application can be started in the command line (from the pom directory):

mvnDebug clean install tomcat7:run-war

Or for jetty:

mvnDebug clean install jetty:run-war

With the application running in debug mode from the command line (you don't need to run it from Intellij), do a remote debugging configuration similar to what you posted and the breakpoint should be hit.

If you use Intellij Ultimate Edition then this is not necessary, because you can create a server configuration for Tomcat or any other server and deploy the application in a fully integrated way, with debugging and hot deployment handled transparently.

There is a 30 day trial where you can evaluate this feature and others.

Angular University
  • 42,341
  • 15
  • 74
  • 81
  • This is not the current issue, the application itself is the server. But we also have a client application that is run on Tomcat7, so eventually debugging it will become an issue. Then I will try this solution. – mirelon Mar 13 '14 at 10:19
  • It really worked (one-click debugging) - all we had to do was to modify the goal from `tomcat7:redeploy` to `tomcat7:run-war` and turn off system tomcat server. And modify the `webappDirectory` in pom.xml. – mirelon Mar 17 '14 at 11:00