If you can't get a FileDescriptorSet ahead of time you'll need to
create one by calling out to protoc at run time...

The optimal thing would be to pre-compile the proto files into
FileDescriptorSet protobuf files and then have those available to you
at runtime so you dont have to make System.exec calls - and as such
become somewhat platform dependent.

The call you want to make is in the form
protoc --descriptor_set_out=[filename] --include_imports <proto file>

You probably want the include imports directive.

You can do this with System.exec() from java at runtime.

You can then load the resultant file (which will be a

Here's a freebie:

        final FileDescriptorSet fds = FileDescriptorSet.parseFrom(new
FileInputStream(myFile)); // load the file however you feel
appropriate - but close your streams : )
            // special note - if you have custom options, you will
need to create an ExtensionRegistry with them and use it while parsing
the FileDescriptorSet
        try {
            Map<String, FileDescriptor> fdl = new HashMap<String,
            FileDescriptor current = null;
            for (FileDescriptorProto fdp : fds.getFileList()) {
                final List<String> dependencyList =
                final FileDescriptor[] fda = new

                for (int i = 0; i < fda.length; i++) {
                    FileDescriptor fddd =
                    if (fddd == null) {
                        // missing imports! - this should not happen
unless you left off the --include_imports directive
                    } else {
                        fda[i] = fddd;
                current = FileDescriptor.buildFrom(fdp, fda);
                fdl.put(current.getName(), current);

            // the "fdl" object now has all the descriptors - grab the
one you need.

        } catch (DescriptorValidationException e) {
            // panic ?

By the way... for being pretty new to protobufs you're playing with
some pretty advanced features using the Descriptors at run-time.

Handy warning... depending on how you move objects around in java -
you may want to be aware the DynamicMessage, unlike GeneratedMessage,
is not Serializable. (this may not matter - but it can trip you up if
you're using JMS or some other java mechanism to send data and didn't
build the protos first).  There are good reasons for this, so no it
can't be avoided.

On Oct 5, 1:12 pm, Dale Durham <geny...@gmail.com> wrote:
> Hi All,
> I am fairly new to protobufs, so please bear with me. I am receiving
> protobuf messages that were compiled for C++ and I need to read them
> in Java. They are created WITHOUT descriptors by the provider and I
> cannot change that but I do have access to the base .proto files that
> the messages were based on.
> My questions are:
> 1) What is the best/fastest way to generate the file descriptor(s)
> into Java? This should be equal to calling importer.import in C++
> based on what I have read.
> 2) I assume that question 1 will involve a call out to protoc from
> Java. If so, how should that look in general and what command/options
> should be used? My goal is to get the file descriptor, after which I
> can get everything else from within Java
> 3) Is there any way to do this without the protoc callout? I am pretty
> sure there is not, but since I am here, I'll ask.
> I should mention that I can already get the routing key from within
> Java without the descriptor, so I can parse that to determine what
> base proto file that I need the descriptor for.
> Thanks in advance,
> Dale

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to