Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:协议缓冲区不生成解析函数_Java_Protocol Buffers - Fatal编程技术网

Java:协议缓冲区不生成解析函数

Java:协议缓冲区不生成解析函数,java,protocol-buffers,Java,Protocol Buffers,我有以下.proto文件: message MediatorMessageMsg{ required double speed = 1; required double heading = 2; required string sender = 3; } 我使用EclipseMars和ProtocolBuffer2.5.0版本。它生成必要的文件(我们不应该编辑),但是我不能使用 写入限制到() parseDelimitedFrom()解析 newBuilder().

我有以下.proto文件:

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

    required string sender = 3;
}
我使用EclipseMars和ProtocolBuffer2.5.0版本。它生成必要的文件(我们不应该编辑),但是我不能使用

  • 写入限制到()
  • 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
被安装在您的
路径中

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"

你的问题缺少一些细节。请你把产生这个警告的代码贴出来好吗。包括所有涉及变量的类型。在你的问题中,你发布到了
MediatorMessageMsg
的定义,在引用的错误中提到了
MediatorMessage
。这不是运行时错误,我甚至无法编译,因为函数无法识别。我给出了无法识别的函数名。好吧,我被名称
MediatorMessage
MediatorMessageMsg
弄糊涂了。现在一切都好了。非常感谢。