gRPC Java is not usable from Java 9 modules
See original GitHub issuePlease answer these questions before submitting your issue.
What version of gRPC are you using?
1.6.1
What JVM are you using (java -version
)?
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
What did you do?
Java 9 allows users to depend on older, non-modularized versions of the libraries by “converting” them to automatic modules. For example, when Maven dependencies on grpc are configured correctly, Java 9 allows me to do the following:
module myapp.foo {
exports com.myapp.foo;
requires grpc.core;
}
This allowed me to use classes from the grpc-core
within my Java 9 module, but unfortunately it wouldn’t compile:
ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.2:compile (default-compile) on project myapp.foo: Compilation failure: Compilation failure:
[ERROR] the unnamed module reads package io.grpc from both grpc.core and grpc.context
[ERROR] module grpc.context reads package io.grpc from both grpc.core and grpc.context
[ERROR] module guava reads package io.grpc from both grpc.core and grpc.context
[ERROR] module error.prone.annotations reads package io.grpc from both grpc.core and grpc.context
[ERROR] module jsr305 reads package io.grpc from both grpc.core and grpc.context
[ERROR] module instrumentation.api reads package io.grpc from both grpc.core and grpc.context
[ERROR] module opencensus.api reads package io.grpc from both grpc.core and grpc.context
[ERROR] module grpc.core reads package io.grpc from both grpc.core and grpc.context
The issue is that Java 9 does not support split packages across modules and this is exactly what’s happening here, as io.grpc
package exists in both grpc-core
and grpc-context
, and to make things worse both grpc-core
and grpc-stub
have transitive dependency on grpc-context
.
I’ve tried excluding grpc-context
from both modules using Maven exclusions, which allowed me to compile successfully, as I don’t have any direct dependencies on grpc-context
. However, I was not able to run the test server, because of the missing Context
class:
java.lang.NoClassDefFoundError: io/grpc/Context
at io.grpc.internal.AbstractServerImplBuilder.build(AbstractServerImplBuilder.java:188)
at io.grpc.testing.GrpcServerRule.before(GrpcServerRule.java:133)
There are several possible solutions, some better than the others:
- Merge classes from
grpc-context
intogrpc-core
and leave empty/dummygrpc-context
module around for backwards compatibility (although most people probably do not depend on it directly). - Do the same as above, but get rid of unnecessary
grpc-context
module. - Rename the
io.grpc
package ingrpc-context
toio.grpc.context
, which would eliminate split package issue, but would break existing code that uses classes from the current location.
In any case, I’m happy to help do the work, but someone will need to decide which approach to take.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:44
- Comments:41 (15 by maintainers)
Top GitHub Comments
Users have requested support for Java 11 in Apache Beam and since it uses grpc it would need for grpc libraries to be compatible with Java 11. Hopefully this request helps increase the priority for fixing this.
In the meantime, we have repackaged
grpc-java
(version 1.22.1) as a Java 9+ module as part of Helidon, in order to support our gRPC framework implementation, and have made it available on Maven Central.This module basically merges
grpc-core
,grpc-context
andgrpc-stub
into a single JAR, and adds necessarymodule_info.java
to it.@ejona86 This is not to say that we are not looking forward to a proper Java 9+ module support in
grpc-java
, which will allow us to get rid of this “necessary evil”. But we needed something now…