Google Protocol Buffers Java

Google Protocol Buffers Java

I hope you are familiar with Google Protocol Buffers and now a days its quite popular in the industry. In this tutorial I will show how to user Google Protocol Buffer using Java to read and write messages.

Tool needed:

  • Protocol buffer compiler which is already included in project download link
  • JDK 1.8
  • Eclipse any version
  • Maven latest version
  • Create Maven project name: GoogleProtoBuff and below is final structure:

Google Protocol Buffers Java

  • pom.xml:

  • First thing you need to do is define message formats in a .proto file. In this example I have created three .proto file to understand better how to create it:
  • addressbook.proto:

  • javaHonkDateRangeRequest.proto:

  • javaHonkTest.proto:

  • Next you will have to generate java class out of it, You will need protocol butter compiler which you could download from here but if you download complete project from below download link you don’t need to do it as I have included in the project. Please user command mentioned in “readme.txt” file:

readme.txt:

  • Open command prompt from and use above command individually to generate java file as sample below in my case:

  • Use all three command you will see file generate in below package (Don’t forget to refresh your project to see generate files):

Google Protocol Buffers Java

  • Now read and write Protocol butter object use below class:
  • ConvertToByteAndObject.java:

  • Above I have showed you message conversion only for one protocol message you could use same technique for another protocol message.

Download project: GoogleProtoBuff

Leave a Reply

Your email address will not be published. Required fields are marked *