Skip to content

Cannot use library from Java 9+ due to split package (JSR 305 vs annotations-api) #12011

Closed

Description

@cowwoc

What version of gRPC-Java are you using?

1.71.0

What is your environment?

Microsoft Windows [Version 10.0.26100.3775]
openjdk version "23.0.2" 2025-01-21
OpenJDK Runtime Environment (build 23.0.2+7-58)
OpenJDK 64-Bit Server VM (build 23.0.2+7-58, mixed mode, sharing)

What did you expect to see?

No error

What did you see instead?

Compiler error when I add a dependency on gRPC-Java:

[ERROR] module com.google.protobuf reads package javax.annotation from both annotations.api and jsr305

(com.google.protobuf is not relevant, I get the same error from all modules in my project)

Steps to reproduce the bug

io.grpc:grpc-api:jar:1.71.0:compile depends on com.google.code.findbugs:jsr305:jar:3.0.2:compile but the README.md instructs Java9 users to also add a dependency on:

<dependency> <!-- necessary for Java 9+ -->
  <groupId>org.apache.tomcat</groupId>
  <artifactId>annotations-api</artifactId>
  <version>6.0.53</version>
  <scope>provided</scope>
</dependency>

Both these packages contain a javax.annotation package which means that the package is split across multiple Java Modules which is illegal in Java 9+

Consider dropping the dependency on JSR 305 as it's basically a dead project anyway.

Activity

  1. cowwoc commented on Apr 9, 2025

    @cowwoc
    Author

    This workaround worked for me:

    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-protobuf</artifactId>
      <version>${grpc.version}</version>
      <exclusions>
        <exclusion>
          <groupId>com.google.code.findbugs</groupId>
          <artifactId>jsr305</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    

    Additionally, please replace:

    <dependency> <!-- necessary for Java 9+ -->
      <groupId>org.apache.tomcat</groupId>
      <artifactId>annotations-api</artifactId>
      <version>6.0.53</version>
      <scope>provided</scope>
    </dependency>
    

    with:

    <dependency>
        <groupId>jakarta.annotation</groupId>
        <artifactId>jakarta.annotation-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    

    You'll have to update the package name of the imported annotations but this artifact is much better than Tomcat's. It is the official replacement for javax.annotations and it includes module-info.java for Java9+ users.

  2. changed the title [-]Cannot use library from Java 9+ due to split package (JSR 305 vs org.apache.tomcat:annotations-api)[/-] [+]Cannot use library from Java 9+ due to split package (JSR 305 vs annotations-api)[/+] on Apr 9, 2025
  3. kannanjgithub commented on Apr 15, 2025

    @kannanjgithub
    Contributor

    The gRPC compiler already has an option of specifying to omit emitting javax annotation for Generated and instead use io.grpc.stub.annotations.GrpcGenerated. There is plan to make this the default Generated annotation to be emitted by the codegen plugin.

    Example pom.xml fragment for the codegen plugin:

         <executions>
            <execution>
                <configuration>
                    <pluginParameter>
                        @generated=omit
                    </pluginParameter>
                </configuration>
    
  4. cowwoc commented on Apr 24, 2025

    @cowwoc
    Author

    You're right. This works.

    Are there any plans to remove the dependency on JSR 305?

  5. kannanjgithub commented on May 13, 2025

    @kannanjgithub
    Contributor

    Yes we will make the @generated=omit behavior the default.

  6. added this to the Next milestone on May 13, 2025
  7. kannanjgithub commented on Jun 3, 2025

    @kannanjgithub
    Contributor

    @generated=omit is now the default, fixed in PR #12080.

  8. modified the milestones: Next, 1.74 on Jun 24, 2025
  9. locked as resolved and limited conversation to collaborators on Sep 23, 2025
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions