K
K
Katya Smirnova2021-11-20 00:00:27
Java
Katya Smirnova, 2021-11-20 00:00:27

How to generate Protobuf in Java?

Hello everyone! I am almost new to both Java and Kotlin, but I have been working with buffer protocols for a long time. There is no way to correctly generate files through protoc, as unnecessary subfolders are created. Let's say I want the generated files to be in a package com.example.pb.test, and they are created in com.example.pb.test.test

the Project Structure like this:
61980f8f38b6b716313301.png

Makefile in the pb folder:

compile:
  protoc --java_out=tutorial --kotlin_out=tutorial test.proto


test.proto
syntax = "proto3";

package tutorial;

option java_package = "tutorial";
option java_outer_classname = "AddressBookProtos";

message Person {
  optional string name = 1;
  optional int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    optional string number = 1;
    optional PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}


As a result, this is created here:
6198103bd5ce9177056058.png

Please help!

PS I also changed java_package to com.lightswitch.contesttest.pb.tutorial, but in this case the whole name is duplicated

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question