Skip to content

[java] PublicMemberInNonPublicType: False positive for main(...) methods #6459

Closed

Description

@uhafner

Affects PMD Version: 7.22.0

Rule: PublicMemberInNonPublicType

Please provide the rule name and a link to the rule documentation:

Description:

The new rule flags the usage of the main method in non public classes. I think only newer JDK version allow main methods to be non-public.

Code Sample demonstrating the issue:

class MyTest {
    public static void main(final String... args)  {
        // main method that is invoked in IntelliJ
    }
}

Expected outcome:

PMD reports a violation, but that's wrong. That's a false positive.

Running PMD through: Maven

Caused by: #6231

Activity

  1. added
    a:false-positive
    on Feb 15, 2026
  2. uhafner commented on Feb 15, 2026

    @uhafner
    Author

    BTW: In order to ignore the violation I need to suppress on the type and not on the affected method, it would be nice if that can be changed as well. (As shown in https://github.com/jenkinsci/analysis-model/blob/666ae125babdfb716e5c1c5198acf01559a7f008/src/test/java/edu/hm/hafner/analysis/IssueTest.java#L362 this works for methods)

  3. changed the title [-]PublicMemberInNonPublicType should ignore `main` methods[/-] [+]`PublicMemberInNonPublicType` should ignore `main` methods[/+] on Feb 15, 2026
  4. changed the title [-]`PublicMemberInNonPublicType` should ignore `main` methods[/-] [+][java] PublicMemberInNonPublicType: False positive for `main(...)` methods[/+] on Feb 16, 2026
  5. UncleOwen commented on Jun 21, 2026

    @UncleOwen
    Member

    Is this really a false positive?

    In Java < 25 (ignoring previews), this isn't a valid entry point.

    In Java >= 25, you can write this as

    class MyTest {
        void main(final String... args)  {
            // main method that is invoked in IntelliJ
        }
    }

    or even

    void main(final String... args)  {
        // main method that is invoked in IntelliJ
    }

    In both cases, I'd say that the warning is justified?

  6. uhafner commented on Jun 21, 2026

    @uhafner
    Author

    Is this really a false positive?

    In Java < 25 (ignoring previews), this isn't a valid entry point.

    It is a valid entry point in Java < 25. Why should this not be possible?

    I am using this part in a lot of my test cases, example: https://github.com/jenkinsci/analysis-model/blob/666ae125babdfb716e5c1c5198acf01559a7f008/src/test/java/edu/hm/hafner/analysis/IssueTest.java#L362

  7. UncleOwen commented on Jun 21, 2026

    @UncleOwen
    Member

    Huh. I could have sworn that main had to be in a public class in java < 25. You're right, I was wrong. This is indeed valid.

  8. added this to the 7.26.0 milestone on Jun 23, 2026
  9. added a commit that references this issue on Jun 23, 2026
    6c60e42
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:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions