[英]Where to find protobuf.jar for using Google protocol buffers in Java?
[英]Importing “google/protobuf/descriptor.proto” in java protocol buffers
我有一個.proto
文件定義,它需要導入"google/protobuf/descriptor.proto"
因為我使用的是Custom Options 。
所以在我的.proto
文件中,我這樣做:
import "google/protobuf/descriptor.proto";
package ...;
...
由於我的文件沒有編譯抱怨依賴項,我得到了一個 descriptor.proto 文件的副本,將它放在我的 proto 文件所在的同一目錄中。
這解決了問題,但我不相信這是正確的方法。 現在, descriptor.proto
與我的.proto
文件一起編譯,從而在運行時生成了 2 個已編譯的descriptor.proto
:
protobuf-java-2.5.0.jar
文件附帶protobuf-java-2.5.0.jar
.proto
文件一起編譯的.proto
我認為應該以某種方式使用--proto-path
選項,但不完全確定正確的方法是什么。
感謝這里的最佳實踐提示!
當我在 .proto 中使用描述符時,我使用它就像
import "google/protobuf/descriptor.proto";
message AddressBook {
required google.protobuf.FileDescriptorSet proto_files = 1;
然后在默認目錄中使用 addressbookSD.proto 生成 java(在 Windows 上):
protoc addressbookSD.proto --java_out=./ --proto_path=./ --proto_path=<protobuf-install-directory>\src
其中 < protobuf-install-directory > 是協議緩沖區安裝目錄。 關鍵是descriptor.proto在
<protobuf-install-directory>\src\google\protobuf
protobuf 導入語句中的級別必須與文件系統中的目錄匹配,就像它們在 java 中一樣。
所以我使用< protobuf-install-directory>\\src作為導入目錄,目錄結構必須是
<protobuf-install-directory>\src
+-- google
+-- protobuf
+-- descriptor.proto
僅供記錄:我最近遇到了同樣的問題,就我而言,我的項目依賴於protobuf-lite
。 顯然,protobuf 需要用於自定義擴展的“完整”protobuf,因此我必須添加依賴項,如下所示:
dependencies {
protobuf "io.grpc:grpc-protobuf:${grpcVersion}"
...
}
請注意,我在我的項目中依賴 gRPC,但這也適用於com.google.protobuf
。
您的協議無法在您系統的默認包含文件夾中找到文件
https://github.com/golang/protobuf/issues/694
apt install protobuf-compiler 沒有把它放在包含文件夾中
如果您在 linux 機器中遇到錯誤,請使用此選項
google/protobuf/descriptor.proto:找不到文件。
google/protobuf/duration.proto:找不到文件。
google/protobuf/timestamp.proto:找不到文件。
在 linux 系統上正確安裝
PROTOC_ZIP=protoc-3.7.1-linux-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.