Schütze Schütze - 1 month ago 8
Java Question

Java: Protocol Buffer does not generate parsing functions

I've the following .proto file:

message MediatorMessageMsg{
required double speed = 1;
required double heading = 2;

required string sender = 3;
}


and I use Eclipse Mars with Protocol Buffer 2.5.0 version. It generates the necessary file (which we are not supposed to edit) however I cannot use the important functions of


  • writeDelimitedTo()

  • parseDelimitedFrom()

  • newBuilder().set...



without these there is simply no point in using the entire thing. I checked the file and I can see parseDelimitedFrom() there, however I cannot call it in my own project (Yes, imported already). When I hover my mouse on the error, it gives me the following:

The method parseDelimitedFrom(ByteArrayInputStream) is undefined for the type MediatorMessage


Anyone has an idea why is this the case?

EDIT: Some more details regarding the question.

I cannot use the function below, for instance, to build my message. It raises an error.

MediatorMessage mediatorMessage = MediatorMessage.newBuilder().


or I cannot do this

ByteArrayOutputStream output = new ByteArrayOutputStream(bufferSize);
mediatorMessage.writeDelimitedTo(output);


or this

ByteArrayInputStream firstInput = new ByteArrayInputStream(buf);
mediatorMessageOne = MediatorMessage.parseDelimitedFrom(firstInput);


So these functions are not recognized for some reason.

Answer

As you still have not answered how your MediatorMessageMsg from the *.proto file becomes MediatorMessage.java find below a stripped down example. Which should point you in the right direction.

Assume following directory and file structure, protoc is assumed to be installed and in your PATH.

bin/
lib/protobuf-java-2.5.0.jar
src/Check.java
MediatorMessage.proto

src/Check.java

import com.google.protobuf.TextFormat;
import sub.optimal.MediatorMessage.MediatorMessageMsg;

class Check {
    public static void main(String...args) {
        MediatorMessageMsg.Builder builder = MediatorMessageMsg.newBuilder();
        MediatorMessageMsg msg = builder.setSpeed(42.0)
                .setHeading(0.0)
                .setSender("foobar")
                .build();

        System.out.println(TextFormat.shortDebugString(msg));
    }
}

MediatorMessage.proto

option java_package = "sub.optimal";
option java_outer_classname = "MediatorMessage";

message MediatorMessageMsg{
    required double speed = 1;
    required double heading = 2;

    required string sender = 3;
}
  • generate Java source from proto file

    protoc --java_out=src/ MediatorMessage.proto
    

    this generates the Java source file src/sub/optimal/MediatorMessage.java.

  • compile the Java sources

    javac -cp lib/protobuf-java-2.5.0.jar:src/. -d bin/ src/Check.java
    

    this generates the files

    bin/Check.class
    bin/sub/optimal/MediatorMessage$1.class
    bin/sub/optimal/MediatorMessage$MediatorMessageMsg$1.class
    bin/sub/optimal/MediatorMessage$MediatorMessageMsg$Builder.class
    bin/sub/optimal/MediatorMessage$MediatorMessageMsg.class
    bin/sub/optimal/MediatorMessage$MediatorMessageMsgOrBuilder.class
    bin/sub/optimal/MediatorMessage.class
    
  • run the simple check

    java -cp lib/protobuf-java-2.5.0.jar:bin/ Check
    

output

speed: 42.0 heading: 0.0 sender: "foobar"