2011-04-08 19 views
1

我想第一次使用協議緩衝區。我正在關注谷歌提供的教程。在* .proto我做出如下:如何從protoc.exe獲取類文件

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; 
} 

然後我用下面的命令進行編譯:

protoc -I=../examples --java_out=src/main/java ../examples/addressbook.proto 

它運行無差錯和產生addressbook.java。但從我所知道的,我需要一個* .class,以便我可以在eclipse環境中使用它。我試圖將其輸出到使用下面的命令* .jar文件:

protoc -I=../examples --java_out=src/main/java/addressbook.jar ../examples/addressbook.proto 

但是導入一個罐子到項目後,日食說我需要的類。我也嘗試在我處於示例目錄中時使用命令將其編譯爲類。

javac *java 

它看到文件,但返回了大量的行,後面跟着「100錯誤」。我知道我可能會完全迷失,甚至沒有接近正確的想法......但任何幫助都會很棒!謝謝!

哦,這裏是調用這個原代碼...

import com.example.tutorial.AddressBookProtos.AddressBook; 
import com.example.tutorial.AddressBookProtos.Person; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.PrintStream; 

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); 
    } 
} 

謝謝!

Link to protobuf tutorial I am using!

回答

0

就包括在Eclipse中的src目錄下的java文件。 Eclipse將它編譯成一個.class文件。

+0

哇!這就是全部...... geez!非常感謝!我需要它! – user697048 2011-04-08 15:29:22