Skip to content

[java] Problem with JDK versions, CloseResource and UnnecessarySuppression #6224

Open

Description

@oowekyala

In my PR #6215 I have the following code:
https://github.com/oowekyala/pmd/blob/eebc986d8061c4abf97fdea856e47c85b8c8d72c/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java#L277-L284

ForkJoinPool implements AutoCloseable since JDK 19. This means if you run PMD with a JRE >= 19, CloseResource will trigger and ask the FJP to be used in a try-with-resources. The minimum version we require to build is Java 17, where this violation does not trigger (and we use JDK 17 in CI). I cannot add a suppression comment, as our CI would fail with UnnecessarySuppression, because the violation does not trigger with JDK 17.

This means basically that merging the PR would make PMD build only with JDK 17, but not any later version.

I'm not sure how to get out of this situation. Maybe UnnecessarySuppression should ignore some suppression comments? For now, UnnecessarySuppression cannot be suppressed the normal way, but having a special string in the comment that makes the rule ignore a violation would achieve the same effect. Eg we could tag the line with // NOPMD-NoUnnecessarySuppression or something like that, and then have UnnecessarySuppression look for this string and never report the line even if the suppression is unused...

The "right way" would be maybe to run PMD with JDK 8 classes in the aux-classpath during our build, so that CloseResource only sees the APIs that we can use...

CC @adangel

Activity

  1. added
    a:bug
    on Nov 12, 2025
  2. jsotuyod commented on Nov 12, 2025

    @jsotuyod
    Member

    This is fixed indeed by setting the right JRE classes as part of the -auxclasspath, this was intended back when we ensured these classes wouldn't collide in runtime and then we even implemented support for JRT (see #4628).

    I'm unsure how we can do that through the Maven plugin 'though, as the auxclasspath is automatically populated from the project's dependencies…

  3. adangel commented on Nov 13, 2025

    @adangel
    Member

    Yes, that's a known problem. I wanted to create awareness first for this problem via #5299, but didn't have time to follow up. I guess, there is still one step missing in how we call PMD from maven-pmd-plugin. I don't know about gradle - I assume we have the same problem there. And the same for ant.
    Although we claim, we have solved the dependency from runtime JDK with PMD 7, we actually didn't: you still need to make sure to run PMD with the same JDK as the version you compile for.
    The actual problems if the JDK versions don't match up are actually seldom and very subtle (#4620 and now this one with AutoCloseable).

    The "right" way to fix this is:

    1. finish (PoC) [java] Verify JDK API Version #5299. Then we can actually see, when this problem appears. Only a warning at the beginning.
    2. fix maven-pmd-plugin. Maybe requiring the users to configure toolchains?
    3. fix gradle
    4. fix ant maybe?
    5. fix eclipse plugin
    6. fix intellij plugin
    7. let PMD fail the analysis, if it finds a mismatch between the Java version on the auxclasspath and the configured java language version.

    Alternatives and workarounds:

  4. jsotuyod commented on Nov 13, 2025

    @jsotuyod
    Member

    Gradle can already handle this, by using the pmdAux configuration when setting these dependencies, such as:

    dependencies {
      pmdAux(…JRE lib reference…)
    }
    

    See https://docs.gradle.org/current/userguide/pmd_plugin.html#sec:pmd_dependency_management

  5. adangel commented on Aug 20, 2026

    @adangel
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions