Note that only the Java source code is placed in the archive you must still compile it separately to produce Java class files. To output to a JAR file, simply provide an output location ending in. When outputting Java code, the protocol buffer compiler's ability to output directly to JAR archives is particularly convenient, as many Java tools are able to read source code directly from JAR files. proto files in a single invocation all output files will be generated at once. However, it will not create build/gen or build they must already exist. The protocol buffer compiler will automatically create the build/gen/com and build/gen/com/example directories if needed. If foo.proto's Java package is com.example and it doesn't enable java_multiple_files and its outer classname is FooProtos, then the protocol buffer compiler will generate the file build/gen/com/example/FooProtos.java. Protoc -proto_path=src -java_out=build/gen src/foo.proto So, for example, let's say you invoke the compiler as follows: The output file is chosen by concatenating the parameter to -java_out=, the package name (with.
The Java package name is chosen as described under Packages, below. Public static void registerAllExtensions( registry) ** Adds all extensions defined in foo.proto} to the given registry. Public static .FileDescriptor getDescriptor() ** Returns a FileDescriptor message describing the contents of foo.proto}. In addition to any nested classes, the wrapper class itself will have the following API (assuming the wrapper class is named Foo and was generated from foo.proto): If foo_bar.proto contains a service called FooService, and java_outer_classname is also set to the string FooService, then the wrapper class will generate a class name of FooServiceOuterClass.If foo_bar.proto contains a message called FooBar, the wrapper class will generate a class name of FooBarOuterClass.If there's a service, enum, or message (including nested types) in the file with the same name, "OuterClass" will be appended to the wrapper class's name.
For example, foo_bar.proto will generate a class name of FooBar. Otherwise, the wrapper class name is determined by converting the. proto file contains a line like the following: The wrapper class's name is chosen as follows. proto file are all nested within the outer wrapper class. Otherwise (that is, when the java_multiple_files option is false which is theĭefault), the aforementioned wrapper class is also used as an outer class, and the generatedĬlasses/enums for each top-level message, enumeration, and service declared in the java file for each top-level message,Įnumeration, and service declared in the. Then the compiler also creates a separate. java file containing a Java class which represents the. proto file input, the compiler creates a wrapper. The parameter to the -java_out= option is the directory where you want the compiler to write your Java output. The protocol buffer compiler produces Java output when invoked with the -java_out= command-line flag. Note that no Java protocol buffer methods accept or return nulls unless otherwise specified. You should read the proto2 language guide and/or proto3 language guide before reading this document. Any differences between proto2 and proto3 generated code are highlighted - note that these differences are in the generated code as described in this document, not the base message classes/interfaces, which are the same in both versions. This page describes exactly what Java code the protocol buffer compiler generates for any given protocol definition.