README.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. Protocol Buffers - Google's data interchange format
  2. Copyright 2008 Google Inc.
  3. This directory contains the Java Protocol Buffers runtime library.
  4. Installation - With Maven
  5. =========================
  6. The Protocol Buffers build is managed using Maven. If you would
  7. rather build without Maven, see below.
  8. 1) Install Apache Maven if you don't have it:
  9. http://maven.apache.org/
  10. 2) Build the C++ code, or obtain a binary distribution of protoc. If
  11. you install a binary distribution, make sure that it is the same
  12. version as this package. If in doubt, run:
  13. $ protoc --version
  14. You will need to place the protoc executable in ../src. (If you
  15. built it yourself, it should already be there.)
  16. 3) Run the tests:
  17. $ mvn test
  18. If some tests fail, this library may not work correctly on your
  19. system. Continue at your own risk.
  20. 4) Install the library into your Maven repository:
  21. $ mvn install
  22. 5) If you do not use Maven to manage your own build, you can build a
  23. .jar file to use:
  24. $ mvn package
  25. The .jar will be placed in the "target" directory.
  26. Installation - 'Lite' Version - With Maven
  27. ==========================================
  28. Building the 'lite' version of the Java Protocol Buffers library is
  29. the same as building the full version, except that all commands are
  30. run using the 'lite' profile. (see
  31. http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  32. E.g. to install the lite version of the jar, you would run:
  33. $ mvn install -P lite
  34. The resulting artifact has the 'lite' classifier. To reference it
  35. for dependency resolution, you would specify it as:
  36. <dependency>
  37. <groupId>com.google.protobuf</groupId>
  38. <artifactId>protobuf-java</artifactId>
  39. <version>${version}</version>
  40. <classifier>lite</classifier>
  41. </dependency>
  42. Installation - Without Maven
  43. ============================
  44. If you would rather not install Maven to build the library, you may
  45. follow these instructions instead. Note that these instructions skip
  46. running unit tests.
  47. 1) Build the C++ code, or obtain a binary distribution of protoc. If
  48. you install a binary distribution, make sure that it is the same
  49. version as this package. If in doubt, run:
  50. $ protoc --version
  51. If you built the C++ code without installing, the compiler binary
  52. should be located in ../src.
  53. 2) Invoke protoc to build DescriptorProtos.java:
  54. $ protoc --java_out=src/main/java -I../src \
  55. ../src/google/protobuf/descriptor.proto
  56. 3) Compile the code in src/main/java using whatever means you prefer.
  57. 4) Install the classes wherever you prefer.
  58. Micro version
  59. ============================
  60. The runtime and generated code for MICRO_RUNTIME is smaller
  61. because it does not include support for the descriptor,
  62. reflection or extensions. Also, not currently supported
  63. are packed repeated elements nor testing of java_multiple_files.
  64. To create a jar file for the runtime and run tests invoke
  65. "mvn package -P micro" from the <protobuf-root>/java
  66. directory. The generated jar file is
  67. <protobuf-root>java/target/protobuf-java-2.2.0-micro.jar.
  68. If you wish to compile the MICRO_RUTIME your self, place
  69. the 7 files below, in <root>/com/google/protobuf and
  70. create a jar file for use with your code and the generated
  71. code:
  72. ByteStringMicro.java
  73. CodedInputStreamMicro.java
  74. CodedOutputStreamMicro.java
  75. InvalidProtocolBufferException.java
  76. MessageMicro.java
  77. WireFormatMicro.java
  78. If you wish to change on the code generator it is located
  79. in /src/google/protobuf/compiler/javamicro.
  80. To generate code for the MICRO_RUNTIME invoke protoc with
  81. --javamicro_out command line parameter. javamciro_out takes
  82. a series of optional sub-parameters separated by comma's
  83. and a final parameter, with a colon separator, which defines
  84. the source directory. Sub-paraemeters begin with a name
  85. followed by an equal and if that sub-parameter has multiple
  86. parameters they are seperated by "|". The command line options
  87. are:
  88. opt -> speed or space
  89. java_use_vector -> true or false
  90. java_package -> <file-name>|<package-name>
  91. java_outer_classname -> <file-name>|<package-name>
  92. opt:
  93. This change the code generation to optimize for speed,
  94. opt=speed, or space, opt=space. When opt=speed this
  95. changes the code generation for strings so that multiple
  96. conversions to Utf8 are eliminated. The default value
  97. is opt=space.
  98. java_use_vector:
  99. Is a boolean flag either java_use_vector=true or
  100. java_use_vector=false. When java_use_vector=true the
  101. code generated for repeated elements uses
  102. java.util.Vector and when java_use_vector=false the
  103. java.util.ArrayList<> is used. When java.util.Vector
  104. is used the code must be compiled with Java 1.3 and
  105. when ArrayList is used Java 1.5 or above must be used.
  106. The using javac the source parameter maybe used to
  107. control the version of the srouce: "javac -source 1.3".
  108. You can also change the <source> xml element for the
  109. maven-compiler-plugin. Below is for 1.5 sources:
  110. <plugin>
  111. <artifactId>maven-compiler-plugin</artifactId>
  112. <configuration>
  113. <source>1.5</source>
  114. <target>1.5</target>
  115. </configuration>
  116. </plugin>
  117. When compiling for 1.5 java_use_vector=false or not
  118. present where the default value is false.
  119. And below would be for 1.3 sources note when changing
  120. to 1.3 you must also set java_use_vector=true:
  121. <plugin>
  122. <artifactId>maven-compiler-plugin</artifactId>
  123. <configuration>
  124. <source>1.3</source>
  125. <target>1.5</target>
  126. </configuration>
  127. </plugin>
  128. java_package:
  129. The allows setting/overriding the java_package option
  130. and associates allows a package name for a file to
  131. be specified on the command line. Overriding any
  132. "option java_package xxxx" in the file. The default
  133. if not present is to use the value from the package
  134. statment or "option java_package xxxx" in the file.
  135. java_outer_classname:
  136. This allows the setting/overriding of the outer
  137. class name option and associates a class name
  138. to a file. An outer class name is required and
  139. must be specified if there are multiple messages
  140. in a single proto file either in the proto source
  141. file or on the command line. If not present the
  142. no outer class name will be used.
  143. Below are a series of examples for clarification of the
  144. various javamicro_out parameters using
  145. src/test/proto/simple-data.proto:
  146. package testprotobuf;
  147. message SimpleData {
  148. optional fixed64 id = 1;
  149. optional string description = 2;
  150. optional bool ok = 3 [default = false];
  151. };
  152. Assuming you've only compiled and not installed protoc and
  153. your current working directory java/, then a simple
  154. command line to compile simple-data would be:
  155. ../src/protoc --javamicro_out=. src/test/proto/simple-data.proto
  156. This will create testprotobuf/SimpleData.java
  157. The directory testprotobuf is created because on line 1
  158. of simple-data.proto is "package testprotobuf;". If you
  159. wanted a different package name you could use the
  160. java_package option command line sub-parameter:
  161. ../src/protoc '--javamicro_out=java_package=src/test/proto/simple-data.proto|my_package:.' src/test/proto/simple-data.proto
  162. Here you see the new java_package sub-parameter which
  163. itself needs two parameters the file name and the
  164. package name, these are separated by "|". Now you'll
  165. find my_package/SimpleData.java.
  166. If you wanted to also change the optimization for
  167. speed you'd add opt=speed with the comma seperator
  168. as follows:
  169. ../src/protoc '--javamicro_out=opt=speed,java_package=src/test/proto/simple-data.proto|my_package:.' src/test/proto/simple-data.proto
  170. Finally if you also wanted an outer class name you'd
  171. do the following:
  172. ../src/protoc '--javamicro_out=opt=speed,java_package=src/test/proto/simple-data.proto|my_package,java_outer_classname=src/test/proto/simple-data.proto|OuterName:.' src/test/proto/simple-data.proto
  173. Now you'll find my_packate/OuterName.java.
  174. As mentioned java_package and java_outer_classname
  175. may also be specified in the file. In the example
  176. below we must define java_outer_classname because
  177. there are multiple messages in
  178. src/test/proto/two-messages.proto
  179. package testmicroruntime;
  180. option java_package = "com.example";
  181. option java_outer_classname = "TestMessages";
  182. message TestMessage1 {
  183. required int32 id = 1;
  184. }
  185. message TestMessage2 {
  186. required int32 id = 1;
  187. }
  188. This could be compiled using:
  189. ../src/protoc --javamicro_out=. src/test/proto/two-message.proto
  190. With the result will be com/example/TestMessages.java
  191. Usage
  192. =====
  193. The complete documentation for Protocol Buffers is available via the
  194. web at:
  195. http://code.google.com/apis/protocolbuffers/