Over a million developers have joined DZone.

Protobuf for Lazy Java Coders

DZone's Guide to

Protobuf for Lazy Java Coders

Don't want to bother with downloading a .proto file, loading up Terminal, and commanding from there? Just have Maven do it for you.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

Sometimes you are just plain lazy and would rather have Maven create the Java files out of .proto. The alternative would have been to download the protoc, fire up the terminal and issue one simple command. Not a big deal at all. But as I said sometimes you are just plain lazy.

So how do you go about it? Follow the steps given below.

Think up a super critical message that you want to send across over the wire.

If you don't know how to write a .proto file, perhaps one of the links below would help.

Below is the super critical hello world that I want to send across.

Create Your Message

syntax = "proto2";

// This is the package where the java source code will be placed. 
option java_package = "le.arn";

// This is the name of the class. 
// If not provided, it will be created as <message name>OuterClass. 
option java_outer_classname = "GreetingProtos";

message Greeting {
  required string greeting = 1;


Create a vanilla Maven project. 

Say thanks to open source and add protoc-jar-maven-plugin to the project. 

<!-- protobuf. (de)serialize proto buffers using java api. -->

This would create the Java files.

Add the Protobuf Libraries

However, they will complain that the Google libraries are not around. 

So you will have to add the protobuf libraries. 

<!-- compile proto file into java files. -->
                <!-- <includeDirectories> <include>src/main/protobuf</include> </includeDirectories> -->
                <!-- Create java files. And put them in the src/main/java directory. -->

The proof of the pudding is in eating it. 


Let's write a test to serialize and deserialize the super critical message.

// 1 : Create a Greeting object using the Protobuf builder.
Builder greetingBuilder = GreetingProtos.Greeting.newBuilder();
Greeting greeting = greetingBuilder.build();
try {
    // 2 : Write the message into a file. Serialize the object.
    FileOutputStream output = new FileOutputStream(SER_FILE);
    // 3 : Deserialize the object from the file.
    Greeting greetingFromFile = Greeting.parseFrom(new FileInputStream(
    logger.debug("We read this from the file {}", greetingFromFile);
    // 4 : All is well?
    assertEquals(HELLO_WORLD, greetingFromFile.getGreeting());
} catch (IOException e) {

That's it. You have a Java project that does the basic steps, i.e. serialization/deserialization, using protobuf. I hope that is helpful. If you know an easier, quicker way, please share it as well. 

Either way, your comments will be appreciated. Please take a couple of minutes and write back. 

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

java ,protobuf ,maven ,tutorial ,file creation

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}