First, use Maven to create a simple Java application.
MVN archetype: generate-dgroupid = org. freebird. protocolbuffer-dartifactid = sample-darchetypeartifactid = Maven-Archetype-Quickstart-dinteractivemode = false
The directory structure is as follows:
$ Tree samplesample ── pom. XML └ ── SRC ├ ── main │ └ ── Java │ └ ── org │ fre── freebird │ └ ── protocolbuffer │ └ ── app. java producer ── test consumer ── Java consumer ── org consumer ── freebird consumer ── protocolbuffer consumer ── apptest. java
Because the Java file generated by the Protocol buffer compiler is external, you need a plug-in to add it to this project.
<Plugin> <groupid> Org. codehaus. mojo </groupid> <artifactid> build-helper-Maven-plugin </artifactid> <version> 1.7 </version> <executions> <execution> <ID> Add-source </ID> <phase> Generate-sources </phase> <goals> <goal> Add-source </goal> </goals> <configuration> <sources> <source>.. /.. /.. /template/Java </source> </sources> </configuration> </execution> </executions> </plugin>
In addition, the Java library of the previously compiled protocol buffer has been deployed on its own nexus private server. Now you can reference it using dependency.
<Dependency> <groupid> com. Google. protobuf </groupid> <artifactid> protobuf-Java </artifactid> <version> 2.4.1 </version> </dependency>
Now we can implement a simple JavaProgram. Reads data from the file specified by the parameter and deserializes it into a user object.
Package org. freebird. protocolbuffer; import org. freebird. sample. userproto. User; import java. Io. fileinputstream; import java. Io. ioexception;/*** sample! **/Public class app {public static void main (string [] ARGs) throws ioexception {system. Out. println ("welcome to the Java sample for protocol buffer! "); User user = user. parsefrom (New fileinputstream (ARGs [0]); system. out. println (user. getname (); system. out. println (user. getemail ());}}
Run the following command (I assume you will use exec plugin)
MVN clean compilemvn Exec: Java
The result is as follows:
Welcome to the Java sample for protocol buffer! Freebirdshu_chen@esri.com
Therefore, the cross-language features of protocol buffer are quite useful.