Java Proto Names

Names that are generated by the immutable API.

This document contains information on what the fully-qualified Java name of a proto is, based on the different proto options. This name corresponds to the package you need to import to use that message.

Immutable API Message Names

The names for protos generated by the immutable API (java_proto_library BUILD target) are listed in the following table.

java_api_versionjava_multiple_filesjava_alt_api_packagejava_packagejava_outer_classnameGenerated full message name
1trueDefined-$java_alt_api_package.$message
1trueNot definedNot definedcom.google.protos.$package.proto2api.$message
1trueNot definedDefined$java_package.proto2api.$message
1falseDefined-Not defined$java_alt_api_package.$derived_outer_class.$message
1falseDefined-Defined$java_alt_api_package.$java_outer_classname.$message
1falseNot definedNot definedNot definedcom.google.protos.$package.proto2api.$derived_outer_class.$message
1falseNot definedNot definedDefinedcom.google.protos.$package.proto2api.$java_outer_classname.$message
1falseNot definedDefinedNot defined$java_package.proto2api.$derived_outer_class.$message
1falseNot definedDefinedDefined$java_package.proto2api.$java_outer_classname.$message
2true-Not defined-com.google.protos.$package.$message
2true-Defined-$java_package.$message
2false-Not definedNot definedcom.google.protos.$package.$derived_outer_class.$message
2false-Not definedDefinedcom.google.protos.$package.$java_outer_classname.$message
2false-DefinedNot defined$java_package.$derived_outer_class.$message
2false-DefinedDefined$java_package.$java_outer_classname.$message

Legend

  • - means either setting or not setting the option will not change the generated full message name.

  • $message is the actual name of the proto message.

  • $package is the name of the proto package. This is the name specified by the package directive in the proto file, which is usually at the top of the file.

  • $derived_outer_class is a name generated from the proto file name. Generally it’s computed by removing punctuation from the file name and converting it to CamelCase. For example, if the proto is foo_bar.proto, the $derived_outer_class value is FooBar.

    If the generated class name would be the same as one of the messages defined in the proto file, derived_outer_class has OuterClass appended to it. For example, if the proto is foo_bar.proto and contains a FooBar message, the $derived_outer_class value is FooBarOuterClass. The same is true when using the v1 API, whether or not the class name would be the same as one of the messages defined.

  • All other $names are the values of the corresponding proto2 file options defined in the proto file.

Recommendation

The recommended set of options to use is:

option java_api_version = 2;
option java_multiple_files = true;

With java_multiple_files = true, the generated Java class for each message will be placed in a separate .java file. This makes it much easier to move messages from one .proto file to another. There is also an outer Java class generated for the .proto file itself. (The legend above explains how this outer class name is generated.)