在Eclipse中配置google protobuf(转)
2011-04-20 17:02 Terry_龙 阅读(12241) 评论(1) 编辑 收藏 举报官方文档
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/javatutorial.html
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/reference/java/index.html
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/proto.html
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/reference/java-generated.html
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/encoding.html
1. 下载:
http://code.google.com/p/protobuf/downloads/list
2.解压文件到:
D:\Java\protoc
将descriptor.proto生成java 代码,
D:\Java\protoc>protoc.exe -h
D:\Java\protoc>protoc.exe –java_out=./ D:\Java\protoc\protobuf-2.0.0beta\src\google\protobuf\descriptor.proto
/** 或者 **/
// 解压protoc-2.3.0-win32.zip,里面有个protoc.exe文件,配置到path
dos下执行:protoc --version 查看版本。
// 然后:D:\Java\protoc>protoc --java_out=. descriptor.proto
3.在Eclipse中新建java工程
工程路径在:E:\lizongbo\workspace\protobuf
复制D:\Java\protoc下生成的com目录到E:\lizongbo\workspace\protobuf\src
复制D:\Java\protoc\protobuf-2.0.0beta\java\src\main\java下的com目录到E:\lizongbo\workspace\protobuf\src
4.定义一个proto文件
参考:http://code.google.com/apis/protocolbuffers/docs/proto.html
qquser.txt内容如下:
option java_package = “com.lizongbo.beans”;
option java_outer_classname = “QQUserProtos”;
message QQUser {
required int32 uin = 1 [default = 10000];
required string nick = 2 [default = "n"];
required int32 age = 3 [default = 20];
required int32 faceId = 4 [default = 0];
}
5.生成java代码:protoc.exe –java_out=./ qquser.txt
// protoc --java_out=. qquser.txt
6.测试代码:
package com.lizongbo.beans;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
QQUserProtos.QQUser.Builder builder = QQUserProtos.QQUser.newBuilder();
builder.setAge(30);
builder.setFaceId(2);
builder.setNick(“lizongbo”);
builder.setUin(10000);
QQUserProtos.QQUser user = builder.build();
System.out.println(user.toString());
System.out.println(user.toByteString().toStringUtf8());
}
}
7.例子可以使用了。
|