0

I am trying to implement a shell script that generates the Java classes for a given set of Protobuf files. The generated classes are then grouped into a JAR which is to be imported by any Java project that needs to use these protobuf generated classes or gRPC stubs.

The script roughly looks like bellow:

# Generate java classes from .proto files
protoc -I ${PROTO_REPO_PATH} 
    --plugin=protoc-gen-grpc-java=$GEN_GRPC_JAVA_PATH \
    --grpc-java_out=$JAVA_OUT --java_out=$JAVA_OUT \
    $FILES
  
JAR_FILE="$JAVA_OUT/$SERVICE_NAME-$API_VERSION.jar"

# Create JAR from generated classes
jar cf "$JAR_FILE" -C "$JAVA_OUT" .
  
# Install JAR in local repository
  mvn install:install-file -Dfile=$JAR_FILE -Dpackaging=jar -DgroupId=foo.grpc -DartifactId=$SERVICE_NAME-proto -Dversion=1.0

# Clean generated java code
rm -r $JAVA_OUT

After execution, the JAR is present in ~/.m2/repository and, using jar tf foo-service-1.0.jar, I verified that all the generated classes are inside it.

Yet I cannot import any of those classes in a Java project on any IDE I tried (i.e. IntelliJ, VScode). The JAR appears empty inside the IDE browser yet autocompletion works when importing the parrent packages but fails when trying to import a specific class.

I tried proposed answers on this similar but different question but without any success.

foshan
  • 1

2 Answers2

0

I don't see a compile step to make .class from .jar. Are there any .class files in the jar or only .java?

You need something like the following run from the base of the package tree:

javac ./ 

Between the protoc and jar steps

John Williams
  • 4,252
  • 2
  • 9
  • 18
0

Figured the problem was due to missing required dependencies in the produced JAR (i.e. "protobuf-java", "grpc-protobuf", "grpc-stub"). Issue solved by using the "protoc-jar-maven-plugin" which generates the Java classes from the .proto files and include the said dependencies in the JAR, making it possible to use the generated classes that are inside.

foshan
  • 1