Using Protocol Buffers

Here is a quick example to demonstrate how to include protocol buffers in your project.

Here's a simple protocol buffer definition, geo.proto:

message Location {
  optional string name = 1;
  optional double latitude = 2;
  optional double longitude = 3;

And our main Java program,

class Hello {
  public static void main(String[] args) {
    Geo.Location.Builder locationBuilder = Geo.Location.newBuilder();
    locationBuilder.setName("CN Tower");
    Geo.Location location =;

First, a little "project" setup:

export J2OBJC_HOME=~/j2objc    # Change to where the j2objc distribution was unzipped.
ls $J2OBJC_HOME/j2objc         # Fix above definition until this command works.
mkdir java objc classes        # Output directories

Next, use j2objc_protoc to generate the protocol buffers. Generate Java code with --java_out and Objective-C code with --j2objc_out. The value specified with each flag is the output directory for the target language. Both output languages can be generated in the same command.

$J2OBJC_HOME/j2objc_protoc --java_out=java --j2objc_out=objc geo.proto
ls java
ls objc
Geo.h Geo.m

The generated Java proto files need to be compiled so references to them in Hello are resolved. This is done using javac so they aren't translated to Objective C in the following step.

javac -cp $J2OBJC_HOME/lib/protobuf_runtime.jar -d classes java/*.java
ls classes/
Geo$1.class                 Geo$Location$Builder.class  Geo$LocationOrBuilder.class
Geo$Location$1.class        Geo$Location.class          Geo.class

Translate the Java sources as usual, adding the compiled java protos to the classpath.

$J2OBJC_HOME/j2objc -cp classes:$J2OBJC_HOME/lib/protobuf_runtime.jar -d objc
ls objc
Geo.h   Geo.m   Hello.h Hello.m

Now we have all of our Objective-C sources to compile and link. You'll need to link with the libprotobuf_runtime.a library to include the protobuf runtime.

$J2OBJC_HOME/j2objcc -lprotobuf_runtime -o hello objc/*.m
./hello Hello
name: "CN Tower"
latitude: 43.6412
longitude: -79.3884