Using Protocol Buffers
Stay organized with collections
Save and categorize content based on your preferences.
Here is a quick example to demonstrate how to include protocol buffers in your project.
Here's a simple protocol buffer definition, geo.proto
:
syntax = "proto2";
message Location {
optional string name = 1;
optional double latitude = 2;
optional double longitude = 3;
}
And our main Java program, Hello.java
:
class Hello {
public static void main(String[] args) {
Geo.Location.Builder locationBuilder = Geo.Location.newBuilder();
locationBuilder.setName("CN Tower");
locationBuilder.setLatitude(43.6412172);
locationBuilder.setLongitude(-79.3884058);
Geo.Location location = locationBuilder.build();
System.out.println(location.toString());
}
}
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
Geo.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 -source 1.8 -target 1.8 -bootclasspath $J2OBJC_HOME/lib/jre_emul.jar -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 Hello.java
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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThis guide demonstrates how to use protocol buffers in a Java project and translate it to Objective-C using j2objc.\u003c/p\u003e\n"],["\u003cp\u003eIt involves defining a protocol buffer in a .proto file, generating Java and Objective-C code using j2objc_protoc, and compiling the generated code.\u003c/p\u003e\n"],["\u003cp\u003eThe guide includes setting up the project environment, compiling Java and Objective-C files, and linking with the protobuf runtime library.\u003c/p\u003e\n"],["\u003cp\u003eThe final output showcases the successful execution of the program, printing the location details defined in the protocol buffer.\u003c/p\u003e\n"]]],["The process involves defining a protocol buffer (`geo.proto`) with a `Location` message, then creating a Java program (`Hello.java`) that utilizes it. `j2objc_protoc` generates Java and Objective-C code from the proto file. Java proto files are compiled with `javac`. Then, `j2objc` translates the Java program to Objective-C, with compiled proto classes added to the classpath. Finally, `j2objcc` compiles and links the Objective-C code, including the `protobuf_runtime` library, to create an executable.\n"],null,["# Using Protocol Buffers\n\nHere is a quick example to demonstrate how to include protocol buffers in your project.\n\nHere's a simple protocol buffer definition, `geo.proto`: \n\n syntax = \"proto2\";\n\n message Location {\n optional string name = 1;\n optional double latitude = 2;\n optional double longitude = 3;\n }\n\nAnd our main Java program, `Hello.java`: \n\n class Hello {\n public static void main(String[] args) {\n Geo.Location.Builder locationBuilder = Geo.Location.newBuilder();\n locationBuilder.setName(\"CN Tower\");\n locationBuilder.setLatitude(43.6412172);\n locationBuilder.setLongitude(-79.3884058);\n Geo.Location location = locationBuilder.build();\n System.out.println(location.toString());\n }\n }\n\nFirst, a little \"project\" setup: \n\n export J2OBJC_HOME=~/j2objc # Change to where the j2objc distribution was unzipped.\n ls $J2OBJC_HOME/j2objc # Fix above definition until this command works.\n mkdir java objc classes # Output directories\n\nNext, use `j2objc_protoc` to generate the protocol buffers. Generate Java code with `--java_out` and\nObjective-C code with `--j2objc_out`. The value specified with each flag is the output directory for\nthe target language. Both output languages can be generated in the same command. \n\n $J2OBJC_HOME/j2objc_protoc --java_out=java --j2objc_out=objc geo.proto\n ls java\n Geo.java\n ls objc\n Geo.h Geo.m\n\nThe generated Java proto files need to be compiled so references to them in `Hello` are resolved.\nThis is done using `javac` so they aren't translated to Objective C in the following step. \n\n javac -source 1.8 -target 1.8 -bootclasspath $J2OBJC_HOME/lib/jre_emul.jar -cp $J2OBJC_HOME/lib/protobuf_runtime.jar -d classes java/*.java\n ls classes/\n Geo$1.class Geo$Location$Builder.class Geo$LocationOrBuilder.class\n Geo$Location$1.class Geo$Location.class Geo.class\n\nTranslate the Java sources as usual, adding the compiled java protos to the classpath. \n\n $J2OBJC_HOME/j2objc -cp classes:$J2OBJC_HOME/lib/protobuf_runtime.jar -d objc Hello.java\n ls objc\n Geo.h Geo.m Hello.h Hello.m\n\nNow we have all of our Objective-C sources to compile and link. You'll need to link with the\nlibprotobuf_runtime.a library to include the protobuf runtime. \n\n $J2OBJC_HOME/j2objcc -lprotobuf_runtime -o hello objc/*.m\n ./hello Hello\n name: \"CN Tower\"\n latitude: 43.6412\n longitude: -79.3884"]]