1

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.

2
  • Your question is missing some details. Could you please post the code which produces this warning. Including the type of all involved variables. In your question you posted to definition of MediatorMessageMsg and in the cited error it mention MediatorMessage. Commented Oct 25, 2016 at 6:22
  • This is not a runtime error, I cannot even compile since the functions are not recognized. And I gave the function names which are not recognized. Commented Oct 26, 2016 at 8:49

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Okay I was confused by the names MediatorMessage and MediatorMessageMsg. Now everything works. Thank you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.