编译 Java 运行时库时出现 Protobuf 错误

Protobuf error when compiling Java runtime library

我正在尝试为 Protocol Buffers. I've got errors on both Windows and OS X by following the README instruction under the Java directory in the Protocol Buffers project (which is cloned from https://github.com/google/protobuf 构建 Java 运行 时间库。

我在protobuf/java目录运行mvn test时出现如下错误

来自 OS X

的错误
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (generate-sources) on project protobuf-java: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] /Users/wangsa/Work/ProtoBuffer/protobuf/java/core/generate-sources-build.xml:4: Execute failed: java.io.IOException: Cannot run program "/Users/wangsa/Work/ProtoBuffer/protobuf/java/core/../../src/protoc" (in directory "/Users/wangsa/Work/ProtoBuffer/protobuf/java/core"): error=2, No such file or directory

来自 Windows

的错误
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.google.protobuf:protobuf-java:bundle:2.6.1
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 57, column 15
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-surefire-plugin is missing. @ line 64, column 15
[WARNING] 'build.plugins.plugin.version' for org.apache.felix:maven-bundle-plugin is missing. @ line 143, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Protocol Buffer Java API 2.6.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (generate-sources) @ protobuf-java ---
[INFO] Executing tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.030 s
[INFO] Finished at: 2016-06-07T10:03:48+12:00
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run (generate-sources) on project protobuf-java: An Ant BuildException has occured: Execute failed: java.io.IOException: Cannot run program "..\src\protoc": CreateProcess error=2, The system cannot find the file specified -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

更多详情

谁能看出哪里出了问题?

编辑: 执行此构建过程对我来说是很新的,我假设如果此过程成功完成,我将获得一个 .jar 文件,我可以将其包含在我的 android 程序中,以便使用从我的 proto 文件生成的 protobuf 方法序列化数据并通过 TCP 发送,这是我的最终目标。

在我的项目中遇到了同样的问题,要解决这个问题,请在 PATH 环境变量中添加 protoc bin 目录。

有时即使重启后eclipse也不会选择添加的路径,如果发生这种情况只需删除工作区中的.metadata目录即可。

现代 OS 可以立即识别环境变量的变化,但 eclipse 需要意识到这一点,它在大多数时间第一次加载后从缓存中读取。

经过以下更改终于让它工作了:

更改 1: 我更改为使用来自 https://github.com/google/protobuf/releases/tag/v2.6.1 instead of https://github.com/google/protobuf/tree/v2.6.1/java

的 java 来源

更改 2: 对于 Windows 机器我需要将 protoc.exe 复制到位于 [=26= 的 src 文件夹中]java 文件夹的父文件夹。之前我将 protoc.exe 复制到 java 文件夹内的 src 中,但没有用。

之后我 运行 mvn test 在 java 文件夹中,一切正常。我能够使用 mvn install 生成 .jar 文件,然后根据 README 文件 mvn package 生成 .jar 文件。

注意:我不明白为什么来自 protobuf/tree/v2.6.1 的代码不起作用,但是因为在 github 上,构建状态也显示失败(截至 6 月 7 日),所以我想这就是原因。

我遇到了同样的问题。进行以下更改以使其正常工作:

  1. 从以下位置下载二进制文件 link - 与 您正在尝试编译的一个(我使用的是 3.2.0): https://repo1.maven.org/maven2/com/google/protobuf/protoc/

  2. 将可执行文件从 protoc-3.2.0-windows-x86_64.exe 重命名为 protoc.exe

  3. 运行 来自 ..\protocol_buf\protobuf-3.2.0\java 文件夹的 mvn 测试

我在尝试 "mvn install" 时也遇到了这个问题。

我收到此错误:protoc 执行失败,因为:null: IllegalArgumentException

为了修复,我做了以下操作:

mvn -Dprotoc.dir=<your protoc.exe bin directory> install

成功了!!