这里不做protobuf的介绍,只是分享一下自己在学习过程中搭建环境的方法。
1.下载jar包: protobuf-java-2.3.0.jar(见附件)
2.下载编译器protoc.ext (见附件)
3. 在eclipse中添加Proto buffers插件:
Help >> Install New Software
The update site URL is:
http://protobuffers-eclipse-plugin.googlecode.com/svn/trunk/site
4. 安装后配置编译器
![](https://i-blog.csdnimg.cn/blog_migrate/6e5d3932379321258acef877d8cbb9e4.jpeg)
5. 在eclipse中新建项目test_protobuf, 将jar文件添加到项目中,添加文件夹proto, 在proto中添加proto文件 addressbook.proto(来至于官网)
// See README.txt for information and build instructions.
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2; // Unique ID number for this person.
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person person = 1;
}
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2; // Unique ID number for this person.
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person person = 1;
}
6. 编译文件生成java文件
1)通过eclipse直接编译
![](https://i-blog.csdnimg.cn/blog_migrate/fbc571aef10e339050610c2c8a197762.jpeg)
在proto文件夹下面会生成对于的java文件
2)通过命令行编译,在命令行中将路径跳转到exe的编译器下,输入如下命令
C:\Linda\mydocument\protobuf>
protoc -I=C:\workspace3.7\test_protobuf\proto --jav
a_out=C:\workspace3.7\test_protobuf\src C:\workspace3.7\test_protobuf\proto\addr
essbook.proto
a_out=C:\workspace3.7\test_protobuf\src C:\workspace3.7\test_protobuf\proto\addr
essbook.proto
通过命令行生成的java文件在指定的src目录下,上图中src目录下的java文件就是通过命令行获得。
以上两种方式都可以得到相应的java文件。
这样环境就搭建完成了,剩下的就可以根据官网示例文档操作。
转载于:https://blog.51cto.com/angelgirl/1154252