Problems using protobufs with java and scala

2019-01-26 19:37发布

问题:

I have a file xxx.proto. I downloaded the protobuf compiler and installed it. Then I issued this command

protoc --java_out=./ xxx.proto

and it generated my xxx.java

Now I want to compile this file into a class file which I can use with Scala.

javac xxx.java

Which gives me this error

xxx.java:7: package com.google.protobuf does not exist
      com.google.protobuf.ExtensionRegistry registry) {
                         ^
xxx.java:12450: package com.google.protobuf.Descriptors does not exist
  private static com.google.protobuf.Descriptors.Descriptor
                                                ^
xxx.java:12453: package com.google.protobuf.GeneratedMessage does not exist
    com.google.protobuf.GeneratedMessage.FieldAccessorTable

...
...
...

100 errors

Now I guessed, it doesnt have the package.

So I copied the class files of package com.google.protobuf into the same folder where xxx.java exists. Note - I didnt compile this package. I downloaded the jar from another extension which had the jar files. So I extracted them. Now my current path where xxx.java resides has com/google/protobuf/ *.class of protobuf library.

I issued the javac command again.

This time I got a different set of errors -

    xxx.java:10: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
          extends com.google.protobuf.MessageOrBuilder {
                                     ^
    xxx.java:215: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
            extends com.google.protobuf.MessageOrBuilder {
                                       ^
    xxx.java:608: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
            extends com.google.protobuf.MessageOrBuilder {
                                       ^
    xxx.java:1017: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
            extends com.google.protobuf.MessageOrBuilder {

..... 100 errors

I even tried to compile the source files which came with google protobufs. The generated java classes are giving the same errors.

Any ideas what to do ??

Answer

Okay. Thanks everyone.

The main problem is that protocol buffers compiler package from google doesnt by default create the java library. I assumed that it does and installs it. It actually does if you are running Maven. But i didnt have maven

So i compiled the code in /java/src and used the jar. ^

回答1:

When compiling, you need to have protobuf lib on your classpath. All those missing packages and classes are from protobuf lib.

Find protobuf jar and use

javac -cp path/to/protobuf.jar xxx.java


回答2:

You are new to Java, right?

What is missing is the java library file (extension .jar) for protobuf. It has to be in the "classpath" for the java compiler and the java runtime. protoc generates your classes, but it depends on the protobuf library.

javac xxx.java -cp PATH_TO_PROTOBUF/protobuf-java-2.3.0.jar

At runtime

java mypackage.MyMain -cp PATH_TO_PROTOBUF/protobuf-java-2.3.0.jar


回答3:

You may need to use version 2.4.1 (or 2.4+, at least) of the protobuf kit, including making sure that you update protoc (the protobuf compiler) and recompile your proto definition using the new protoc. (In other words, everything has to be the same version:

  • the protobuf-vn.n.n.jar file;
  • the protoc compiler; and
  • the output of compiling your .proto files with protoc.

One I got everything synched, I began to move forward with a Clojure project I'm looking at. You may be encountering the same version skew problem.



回答4:

protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto

you can download protoc.exe (new release) from>>.

https://code.google.com/p/protobuf/downloads/detail?name=protoc-2.5.0-win32.zip&can=2&q=

in your *.proto file you correctly config

option java_package = "com.example.package";
option java_outer_classname = "class name";


回答5:

One can install the protobuf jar file using the ubuntu a

apt-get install libprotobuf-java

This will copy the protobuf-java-2.4.1.jar under /usr/share/java/

Hope this helps