Echo
Echo

Reputation: 3029

Protocol Buffer: How to import?

I have 2 .proto files :

First file:

package com.test.model;

message ProtoModel  {
    required CustomObj custom=1;
}

Second file:

package com.test.model;

message CustomObj {
    required string smth=1;
}

The issue here is that "CustomObj" is said to be "unresolved reference" . Thus, I've tried to import the second file into first file:

import "com/test/model/firstFile.proto"

package com.test.model;    

message ProtoModel  {
    required CustomObj custom=1;
}

I still get the same issue !!

Upvotes: 3

Views: 13757

Answers (1)

laher
laher

Reputation: 9110

The import statement is the folder relative to the place where you invoke protoc. It looks like you have treated it as relative to the package instead.

e.g. if (like me) you store both files in src/main/resources, you'd invoke protoc as follows:

protoc src/main/resources/firstFile.proto src/main/resources/secondFile.proto --java_out=src/generated/java

and your import statement would be import "src/main/resources/firstFile.proto"

If you want to store the files in subfolders according to package name, then you just add this accordingly, after the top-level foldername.

HTH

Upvotes: 6

Related Questions