我在需要使用 Protobuf 的 Java 中使用 Play 框架(它使用 SBT 构建工具)。所以我有 xxx.proto
文件。我得到了二进制协议(protocol)编译器并添加到类路径中。所以我明白了-
protoc --version
libprotoc 3.1.0
我使用 - 编译了 xxx.proto 文件
protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/xxx.proto
这样就生成了xxx.java文件。
现在我正在编译这个 xxx.java 文件(使用 sbt 构建工具的项目)
[error] /my_project/app/helpers/xxx.java:7: package com.google.protobuf does not exist
[error] com.google.protobuf.ExtensionRegistryLite
[error] /my_project/app/helpers/xxx.java:11: package com.google.protobuf does not exist
[error] com.google.protobuf.ExtensionRegistry
[error] /my_project/app/helpers/xxx.java:6182: package com.google.protobuf.Descriptors does not exist
[error] com.google.protobuf.Descriptors.Descriptor
[error] /my_project/app/helpers/xxx.java:6185: package com.google.protobuf.GeneratedMessageV3 does not exist
[error] com.google.protobuf.GeneratedMessageV3.FieldAccessorTable`
我在我安装的库中看到 - com.google.protobuf
jar 在那里。
我的 xxx.proto
看起来如下 -
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: xxx.proto
public final class xxx {
private xxx() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
......
在生成 xxx.java
文件时我是否遗漏了什么?
我应该如何修复这些编译错误?
最佳答案
您需要确保您使用的是完全相同版本的 protoc
和 libprotobuf.jar
。从你写的内容来看,听起来你使用的是 protoc 版本 3.1.0 但 libprotobuf 2.5.0。你需要改用 libprotobuf 3.1.0,否则你会得到像你引用的那样的编译错误。
关于java - 使用 SBT 构建工具在 Java 中编译 Protobufs 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792879/