Java:Protocol Buffer不生成解析函数

标签 java protocol-buffers

我有以下 .proto 文件:

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

    required string sender = 3;
}

我使用 Eclipse Mars 和 Protocol Buffer 2.5.0 版本。它生成必要的文件(我们不应该编辑该文件),但是我无法使用

的重要功能
  • writeDelimitedTo()
  • parseDelimitedFrom()
  • newBuilder().set...

如果没有这些,使用整个东西就没有意义。我检查了该文件,我可以在那里看到 parseDelimitedFrom() ,但是我无法在我自己的项目中调用它(是的,已经导入)。当我将鼠标悬停在错误上时,它会显示以下内容:

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

有人知道为什么会出现这种情况吗?

编辑:有关该问题的更多详细信息。

例如,我无法使用下面的函数来构建我的消息。它会引发错误。

MediatorMessage mediatorMessage = MediatorMessage.newBuilder().

否则我无法做到这一点

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

或者这个

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

因此这些函数由于某种原因无法被识别。

最佳答案

由于您还没有回答 *.proto 文件中的 MediatorMessageMsg 如何变成 MediatorMessage.java,请在下面找到一个精简示例。这应该为您指明正确的方向。

假设以下目录和文件结构,假定已安装 protoc 并位于您的 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;
}
  • 从 proto 文件生成 Java 源代码

    protoc --java_out=src/ MediatorMessage.proto
    

    这会生成 Java 源文件 src/sub/optimal/MediatorMessage.java

  • 编译 Java 源

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

    这会生成文件

    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
    
  • 运行简单检查

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

输出

speed: 42.0 heading: 0.0 sender: "foobar"

关于Java:Protocol Buffer不生成解析函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40221531/

相关文章:

具有 bazel 构建的 protobuf 依赖项的 Google 云函数

java - AtomicBoolean vs Synchronized block ,有什么区别

java - Java 中的线程运行一段时间后能否获得实际使用的堆栈大小?

java - 是什么导致 ConcurrentHashMap 调整大小

java - SQLServer 的 Hibernate 配置 - 驱动程序类

c++ - 在 CMake 中使用 protobuf 作为 ExternalProject

python - 在 protobuf 中需要 `oneof`?

java - 如何向 JTextArea 添加水平和垂直滚动条(java)

parsing - 可以在不同语言之间序列化/解析 Google Protocol Buffers 吗?

protobuf-net 保留 future 领域