zno2

google/protobuf hello world

/(ㄒoㄒ)/~~ 官网被墙

1. github > Search > protobuf or protocol buffers

2.https://github.com/google/protobuf

3. releases 

4. protoc-3.0.0-alpha-2-win32.zip  

readme.txt

Protocol Buffers - Google's data interchange format
Copyright 2008 Google Inc.
https://developers.google.com/protocol-buffers/

This package contains a precompiled Win32 binary version of the protocol buffer
compiler (protoc).  This binary is intended for Windows users who want to
use Protocol Buffers in Java or Python but do not want to compile protoc
themselves.  To install, simply place this binary somewhere in your PATH.

This binary was built using MinGW, but the output is the same regardless of
the C++ compiler used.

You will still need to download the source code package in order to obtain the
Java or Python runtime libraries.  Get it from:
  https://github.com/google/protobuf/releases/

将 protoc.exe 放至 C:\WINDOWS\system32

cmd

protoc --version

libprotoc 3.0.0

protoc -h

Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
  --version                   Show version info and exit.
  -h, --help                  Show this text and exit.
  --encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode=MESSAGE_TYPE       Read a binary message of the given type from
                              standard input and write it in text format
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode_raw                Read an arbitrary protocol message from
                              standard input and write the raw tag/value
                              pairs in text format to standard output.  No
                              PROTO_FILES should be given when using this
                              flag.
  -oFILE,                     Writes a FileDescriptorSet (a protocol buffer,
    --descriptor_set_out=FILE defined in descriptor.proto) containing all of
                              the input files to FILE.
  --include_imports           When using --descriptor_set_out, also include
                              all dependencies of the input files in the
                              set, so that the set is self-contained.
  --include_source_info       When using --descriptor_set_out, do not strip
                              SourceCodeInfo from the FileDescriptorProto.
                              This results in vastly larger descriptors that
                              include information about the original
                              location of each decl in the source file as
                              well as surrounding comments.
  --error_format=FORMAT       Set the format in which to print errors.
                              FORMAT may be 'gcc' (the default) or 'msvs'
                              (Microsoft Visual Studio format).
  --print_free_field_numbers  Print the free field numbers of the messages
                              defined in the given proto files. Groups share
                              the same field number space with the parent
                              message. Extension ranges are counted as
                              occupied fields numbers.
  --plugin=EXECUTABLE         Specifies a plugin executable to use.
                              Normally, protoc searches the PATH for
                              plugins, but you may specify additional
                              executables not in the path using this flag.
                              Additionally, EXECUTABLE may be of the form
                              NAME=PATH, in which case the given plugin name
                              is mapped to the given executable even if
                              the executable's own name differs.
  --cpp_out=OUT_DIR           Generate C++ header and source.
  --java_out=OUT_DIR          Generate Java source file.
  --javanano_out=OUT_DIR      Generate Java Nano source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR          Generate Ruby source file.

protoc --java_out=. addressbook.proto

[libprotobuf WARNING google/protobuf/compiler/parser.cc:471] No syntax specified for the proto file. Please use 'syntax
= "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)

遭遇以上问题时,请确保proto file 中 已指定syntax ,然后重试。

具体查看 https://github.com/google/protobuf/blob/master/CHANGES.txt

A new notion "syntax" is introduced to specify whether a .proto file
uses proto2 or proto3:

// foo.proto
syntax = "proto3";
message Bar {...}

5. 消息模板文件位置

clone https://github.com/google/protobuf.git 

文件位于 \protobuf\examples

6. maven 依赖

<repositories>
        <repository>
            <id>oschina</id>
            <url>http://maven.oschina.net/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.0.0-alpha-2</version>
        </dependency>
    </dependencies>

7. 完整示例

help tree

tree /F

E:.
└─hello-protocol
    │ ADDRESS_BOOK_FILE
    │ pom.xml
    │
    └─src
        └─main
            └─java
                └─cn
                    └─zxg
                            AddPerson.java
                            AddressBookProtos.java
                            ListPeople.java

 执行AddPerson 参数为 ADDRESS_BOOK_FILE 结果:

ADDRESS_BOOK_FILE: File not found.  Creating a new file.
Enter person ID: 7
Enter name: James
Enter email address (blank for none): 7@7.com
Enter a phone number (or leave blank to finish): 911
Is this a mobile, home, or work phone? work
Enter a phone number (or leave blank to finish): 

执行ListPerson 参数为 ADDRESS_BOOK_FILE 结果:

Person ID: 7
  Name: James
  E-mail address: 7@7.com
  Work phone #: 911
 

 

 

 

 

 

 

posted on 2016-08-06 07:56  zno2  阅读(1044)  评论(0编辑  收藏  举报

导航