7

I am following the link https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/helloworld here to try to get grpc working.

enter image description here

this is the final folder structure.

And my HelloWorldServer.java is the same as https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java, except the package name.

but Intellij tells that it cannot resolve symbol GreeterGrpc.

Could someone help me out of here please?

4 Answers 4

11

Notice that GreeterGrpc.java is under generated-sources, but IntelliJ doesn't know it's a source file.

I see that target/generated-sources/protobuf/java is marked as generated sources root, but target/generated-sources/protobuf/grpc-java is not. Normally IntelliJ picks this up from the configuration in pom.xml. Maybe you don't have something correctly configured there. You could try to reimport the Maven module by right-click on pom.xml, and select Maven and then Reimport. The icon of grpc-java should change to blue with a gear, like java at the same directory level.

If that doesn't work, then you could mark grpc-java as a generated source root manually: right-click on it, select Mark Directory as and then Generated Sources Root.

5

The Protobuf plugin assumes Protobuf files (*.proto) are organized in the same way as Java source files, in sourceSets. The Protobuf files of a sourceSet are compiled in a single protoc run, and the generated files are added to the input of the Java compilation run of that sourceSet ().

sourceSets {
    main {
        java {
            srcDirs 'build/generated/source/proto/main/grpc'
            srcDirs 'build/generated/source/proto/main/java'
        }
    }
}
0

For me, first of all a problem (in Eclipse) was using single quotes around $ variables. My working gradle.build to compile proto files to Java classes in a custom directory was this:

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
    }
    generatedFilesBaseDir = "$projectDir/gensrc"
    generateProtoTasks {
        all()*.plugins { grpc {} }
    }
}

// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
    main {
        java {
            srcDirs "${protobuf.generatedFilesBaseDir}/main/java"
            srcDirs "${protobuf.generatedFilesBaseDir}/main/grpc"
        }
    }
}

Then Eclipse could not tell these were source files. I had to refresh the project:

refresh

Then add the custom files to the build path

build path

0

Just as the quick-start said: Compile the client and server:

$ ./gradlew installDist

then the GreeterGrpc.java will be generated in ./build file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.