Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using protobufs with java and scala

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

like image 534
Aditya Singh Avatar asked Nov 25 '11 07:11

Aditya Singh


People also ask

What languages does Protobuf support?

Protocol buffers currently support generated code in Java, Python, Objective-C, and C++.

Is Protobuf backwards compatible?

Protocol buffers provide a language-neutral, platform-neutral, extensible mechanism for serializing structured data in a forward-compatible and backward-compatible way.

Should I use Protobuf or JSON?

As JSON is textual, its integers and floats can be slow to encode and decode. JSON is not designed for numbers. Also, Comparing strings in JSON can be slow. Protobuf is easier to bind to objects and faster.

Should I use Protobuf?

We recommend you use Protobuf when: You need fast serialisation/deserialisation. Type safety is important. Schema adherence is required.


1 Answers

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
like image 138
Peter Štibraný Avatar answered Sep 30 '22 19:09

Peter Štibraný