Java Tutorial :http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/javatutorial.html
环境准备:
(1)下载protoc代码生成器和源码包:http://code.google.com/p/protobuf/downloads/list
protobuf-2.4.1.tar.bz2
protoc-2.4.1-win32.zip
(2)解压protoc-2.4.1-win32.zip到D:\doc\protobuf\protoc-2.4.1-win32,把protoc.exe添加到PATH环境变量
(3)解压protobuf-2.4.1.tar.bz2到D:\doc\protobuf\protobuf-2.4.1,复制protoc.exe到D:\doc\protobuf\protobuf-2.4.1\src,
进入D:\doc\protobuf\protobuf-2.4.1\java目录,然后执行:mvn -install,会在D:\doc\protobuf\protobuf-2.4.1\java目录下的target目录下面生成protobuf-java-2.4.1.jar包。
[注意]需要在机器上安装了maven。
(4)把protobuf-java-2.4.1.jar添加到classpath。
在D:\doc\protobuf\protobuf-2.4.1\examples里面自带的一个Demo:
(1)切换到D:\doc\protobuf\protobuf-2.4.1\examples,可以看到有一个addressbook.proto
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2;
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;
}
message AddressBook {
repeated Person person = 1;
}
命令行切换到D:\doc\protobuf\protobuf-2.4.1\examples,执行:protoc --java_out=. addressbook.proto
在D:\doc\protobuf\protobuf-2.4.1\examples目录下面会生成com\example\tutorial\AddressBookProtos.java
继续执行:javac -cp D:\doc\protobuf\protobuf-2.4.1\examples\protobuf-java-2.4.1.jar com\example\tutorial\AddressBookProtos.java
会生成一大堆class文件,都是内部类。
(2)在D:\doc\protobuf\protobuf-2.4.1\examples目录下面,有一个AddPerson.java
import com.example.tutorial.AddressBookProtos.AddressBook;
import com.example.tutorial.AddressBookProtos.Person;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
class AddPerson { // This function fills in a Person message based on user
// input.
static Person PromptForAddress(BufferedReader stdin, PrintStream stdout)
throws IOException {
Person.Builder person = Person.newBuilder();
stdout.print("Enter person ID: ");
person.setId(Integer.valueOf(stdin.readLine()));
stdout.print("Enter name: ");
person.setName(stdin.readLine());
stdout.print("Enter email address (blank for none): ");
String email = stdin.readLine();
if (email.length() > 0) {
person.setEmail(email);
}
while (true) {
stdout.print("Enter a phone number (or leave blank to finish): ");
String number = stdin.readLine();
if (number.length() == 0) {
break;
}
Person.PhoneNumber.Builder phoneNumber = Person.PhoneNumber
.newBuilder().setNumber(number);
stdout.print("Is this a mobile, home, or work phone? ");
String type = stdin.readLine();
if (type.equals("mobile")) {
phoneNumber.setType(Person.PhoneType.MOBILE);
} else if (type.equals("home")) {
phoneNumber.setType(Person.PhoneType.HOME);
} else if (type.equals("work")) {
phoneNumber.setType(Person.PhoneType.WORK);
} else {
stdout.println("Unknown phone type. Using default.");
}
person.addPhone(phoneNumber);
}
return person.build();
}
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: AddPerson ADDRESS_BOOK_FILE");
System.exit(-1);
}
AddressBook.Builder addressBook = AddressBook.newBuilder();
// Read the existing address book.
try {
addressBook.mergeFrom(new FileInputStream(args[0]));
} catch (FileNotFoundException e) {
System.out.println(args[0]
+ ": File not found. Creating a new file.");
}
// Add an address.
addressBook.addPerson(PromptForAddress(new BufferedReader(
new InputStreamReader(System.in)), System.out));
// Write the new address book back to disk.
FileOutputStream output = new FileOutputStream(args[0]);
addressBook.build().writeTo(output);
output.close();
}
}
在命令行输入:javac -cp javac -cp D:\doc\protobuf\protobuf-2.4.1\examples\protobuf-java-2.4.1.jar;D:\doc\protobuf\protobuf-2.4.1\examples AddPerson.java
在D:\doc\protobuf\protobuf-2.4.1\examples下面生成:AddPerson.class
在命令行输入:java -cp javac -cp D:\doc\protobuf\protobuf-2.4.1\examples\protobuf-java-2.4.1.jar;D:\doc\protobuf\protobuf-2.4.1\examples AddPerson 1.txt
命令行提示:
1.txt: File not found. Creating a new file.
Enter person ID: 1
Enter name: xujsh
Enter email address (blank for none): xjs@163.com
Enter a phone number (or leave blank to finish): 13612341234
Is this a mobile, home, or work phone? mobile
Enter a phone number (or leave blank to finish): 01012345678
Is this a mobile, home, or work phone? home
Enter a phone number (or leave blank to finish):
在D:\doc\protobuf\protobuf-2.4.1\examples下面生成:1.txt
(3)在D:\doc\protobuf\protobuf-2.4.1\examples目录下面,还有一个ListPeople.java
import java.io.FileInputStream;
import com.example.tutorial.AddressBookProtos.AddressBook;
import com.example.tutorial.AddressBookProtos.Person;
class ListPeople {
// Iterates though all people in the AddressBook and prints
// info about them.
static void Print(AddressBook addressBook) {
for (Person person : addressBook.getPersonList()) {
System.out.println("Person ID: " + person.getId());
System.out.println(" Name: " + person.getName());
if (person.hasEmail()) {
System.out.println(" E-mail address: " + person.getEmail());
}
for (Person.PhoneNumber phoneNumber : person.getPhoneList()) {
switch (phoneNumber.getType()) {
case MOBILE:
System.out.print(" Mobile phone #: ");
break;
case HOME:
System.out.print(" Home phone #: ");
break;
case WORK:
System.out.print(" Work phone #: ");
break;
}
System.out.println(phoneNumber.getNumber());
}
}
}
// Main function: Reads the entire address book from a file and prints all
// the information inside.
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: ListPeople ADDRESS_BOOK_FILE");
System.exit(-1);
}
// Read the existing address book.
AddressBook addressBook = AddressBook.parseFrom(new FileInputStream(
args[0]));
Print(addressBook);
}
}
在命令行输入:javac -cp javac -cp D:\doc\protobuf\protobuf-2.4.1\examples\protobuf-java-2.4.1.jar;D:\doc\protobuf\protobuf-2.4.1\examples ListPeople.java
在D:\doc\protobuf\protobuf-2.4.1\examples下面生成:ListPeople.class
在命令行输入:java -cp javac -cp D:\doc\protobuf\protobuf-2.4.1\examples\protobuf-java-2.4.1.jar;D:\doc\protobuf\protobuf-2.4.1\examples ListPeople 1.txt
输出结果:
Person ID: 1
Name: xujsh
E-mail address: xjs@163.com
Mobile phone #: 13612341234
Home phone #: 01012345678