1

Here are the three files I am working with:

// city.proto
syntax = "proto3";
package city;

message City {

    string cityName = 1;
    string zipCode = 2;
    string countryName = 3;

}

// street.proto
syntax = "proto3";

import "Exercise/city.proto";
package street;

message Street {

    string cityName = 1;
    city.City city = 2;

}

// building.proto
syntax = "proto3";

import "Exercise/street.proto";
package building;

message Building {

    string buildingName = 1;
    string buildingNumber = 2;
    street.Street street = 3;

}

This is my current directory structure:

   - PROTOCOLBUFFERS (folder on desktop)
        - Exercise
           - city.proto
           - street.proto
           - building.proto

This is the command I'm using to generate code from the proto files protoc -I="."/Exercise --java_out=Exercise Exercise/*.proto

I am running this command with my terminal inside the PROTOCOLBUFFERS folder.

What am I doing wrong in the execution of this command? I am on windows. This is the error message I get and online search for it hasn't been useful.

building.proto:3:1: Import "Exercise/street.proto" was not found or had errors.
building.proto:10:5: "street.Street" is not defined.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66

2 Answers2

0

I'm unfamiliar with running protoc on Windows, but...

Try each of these in turn:

  1. Replacing the Linux path separator / with the Windows separator \.
  2. Using absolute paths from the drive root: protoc --proto_path=c:\...\Exercise --java_out=Exercise c:\...\Exercise\*.proto, i.e. replace c:\... with the correct path.
  3. If that doesn't work, replace the single wildcard (*.proto) with full qualified paths to each proto c:\...\Exercise\city.proto c:\...\Exercise\building.proto c:\...\Exercise\street.proto

protoc is "picky". If you need to use a proto_path, you should repeat the appropriate path in subsequent references to proto files.

I am surprised to see that the documentation suggests that "import" is not supported in Java!? I use languages other than Java and would be surprised if this were true, but it is what it says:

https://developers.google.com/protocol-buffers/docs/proto3#importing_definitions

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
0

You should change your import path in the proto files: they are already in the same folder. So change as example:

// street.proto
syntax = "proto3";

import "city.proto";

instead of

// street.proto
syntax = "proto3";

import "Exercise/city.proto";

After this fix, the command generates the files as:

.
├── Exercise
│   ├── building
│   │   └── BuildingOuterClass.java
│   ├── building.proto
│   ├── city
│   │   └── CityOuterClass.java
│   ├── city.proto
│   ├── street
│   │   └── StreetOuterClass.java
│   └── street.proto
└── README.md

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115