9

If you get build errors like these when using protobufs with java, look below.

The method getOptions() from the type Descriptors.Descriptor refers to the missing type MessageOptions

The import com.google.protobuf.DescriptorProtos cannot be resolved

FileDescriptorProto cannot be resolved to a type
4

3 回答 3

36

Ok, the so-called Java tutorial for protobufs doesn't actually mention how to get the protobuf library into your project. It implies that all the code is in your single generated .java file, which would actually be pretty nice, but that isn't case.

Look at the source and you will see references to com.google.protobuf, which you can find in the java/src/main/java directory in the protobuf source. Copy that into your project however, and it will have build errors.

The solution is in the README.txt file. Yeah, maybe I should have read it, but shouldn't all the information you need to get started be in the getting started tutorial? Anyway, do this:

# From the protobuf directory.
cd java
protoc --java_out=src/main/java -I../src ../src/google/protobuf/descriptor.proto

And then copy the java files into your project.

于 2012-07-28T01:46:35.117 回答
0

另一种选择是编辑源中包含的 pom.xml。您可以更改它以在验证生命周期编译 proto 文件并将它们写入源目录。

应用此差异或类似(或创建新的构建配置文件):

$ diff -u ~/Downloads/protobuf-2.6.0/java/pom.xml pom.xml
--- /c/Users/MYNAME/Downloads/protobuf-2.6.0/java/pom.xml     Mon Aug 25 20:52:36 2014
+++ pom.xml     Tue Dec  2 13:51:56 2014
@@ -74,12 +74,12 @@
         <executions>
           <execution>
             <id>generate-sources</id>
-            <phase>generate-sources</phase>
+            <phase>validate</phase>
             <configuration>
               <tasks>
                 <mkdir dir="target/generated-sources" />
-                <exec executable="../src/protoc">
-                  <arg value="--java_out=target/generated-sources" />
+                <exec executable="protoc">
+                  <arg value="--java_out=src/main/java" />
                   <arg value="--proto_path=../src" />
                   <arg value="../src/google/protobuf/descriptor.proto" />
                 </exec>
@@ -92,12 +92,12 @@
           </execution>
           <execution>
             <id>generate-test-sources</id>
-            <phase>generate-test-sources</phase>
+            <phase>validate</phase>
             <configuration>
               <tasks>
                 <mkdir dir="target/generated-test-sources" />
-                <exec executable="../src/protoc">
-                  <arg value="--java_out=target/generated-test-sources" />
+                <exec executable="protoc">
+                  <arg value="--java_out=src/test/java" />
                   <arg value="--proto_path=../src" />
                   <arg value="--proto_path=src/test/java" />
                   <arg value="../src/google/protobuf/unittest.proto" />

现在,您可以运行mvn validate,所有的 proto 文件都将编译到您的项目的源代码中 :)

于 2014-12-02T13:59:37.353 回答
0

https://github.com/google/protobuf/tree/master/java

安装 - 没有 Maven

如果您不想安装 Maven 来构建库,则可以按照这些说明进行操作。请注意,这些说明跳过了运行单元测试,仅描述了如何安装核心 protobuf 库(没有 util 包)。

1) 编译 C++ 代码,或获取 protoc 的二进制分布。如果您安装二进制发行版,请确保它与此软件包的版本相同。如果有疑问,请运行:

$ protoc --version 如果您在未安装的情况下构建了 C++ 代码,编译器二进制文件应位于 ../src 中。

2)调用protoc构建DescriptorProtos.java:

$ protoc --java_out=core/src/main/java -I../src \ ../src/google/protobuf/descriptor.proto 3) 使用您喜欢的任何方式编译 core/src/main/java 中的代码.

4) 在您喜欢的任何地方安装课程。

于 2017-02-23T07:42:40.803 回答