Problems using protobufs with java and scala

2019-01-26 19:29发布

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. ^

5条回答
叼着烟拽天下
2楼-- · 2019-01-26 20:07

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

查看更多
戒情不戒烟
3楼-- · 2019-01-26 20:13

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楼-- · 2019-01-26 20:22

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楼-- · 2019-01-26 20:25

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
查看更多
男人必须洒脱
6楼-- · 2019-01-26 20:27

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
查看更多
登录 后发表回答