Getting IntelliJ to generate Java Sources from Proto files

On my Mac I have libprotoc 27.3 (Protocol Buffers (Protobuf)) installed locally and I am trying to get it to “play nice” with my Java 17 app inside of IntelliJ. My Java app has the following high-level directory structure:

myapp/
    src/
        main/
            java/
            proto/
            resources/

Inside the src/main/proto/ directory is a single file called dostuff.proto and it looks like this:

syntax = "proto3";

package stuffdoer;
option java_multiple_files = true;
option java_package = "com.me.myapp.dostuff.stuffdoer";

message DoStuffRequest{
  bytes image = 1;
}

message DoStuffResponse{
  string stuff_image = 1;
}

service DoStuffDetection{
  rpc GetStuffToDo(DoStuffRequest) returns (DoStuffResponse) {}
}

I believe the idea is that I need to run this tool to generate Java source code that can then be used in other areas of this project. Sure enough, I see that there are Java source files that seem to expect Java code generated by this file. One example is in a Java source file called DoStuffClient that declares the following import statements:

package com.me.myapp.dostuff.stuffdoer.client;

import com.google.protobuf.ByteString;
import com.me.myapp.dostuff.stuffdoer.DoStuffGrpc;
import com.me.myapp.dostuff.stuffdoer.DoStuffRequest;
import io.grpc.StatusRuntimeException;

...

In IntelliJ, I am getting compiler errors because I have not explicitly ran Google Protocol Buffers to read this dostuff.proto and generate the DoStuffGrpc and DoStuffRequest Java sources.

When I run the Maven build locally and run the app on my local machine, it starts up and I am able to manually test the code in the DoStuffClient and verify it works just fine. So I believe Maven is correctly generating the Java sources (defined in dostuff.proto) and making them available to the Java runtime.

However it is annoying to be getting the compiler errors in IntelliJ. What can I do to invoke Google Protocol Buffers from inside IntelliJ so that it generates the Java sources (defined in dostuff.proto) and makes them available to the compile-time path, so that I stop seeing compiler errors when I’m writing code in IntelliJ?

What are the exact steps to take to get IntelliJ generating Java sources from my .proto files and then getting it to link them to the compile-time path so that other source files in the opened project can “see” them?

4

So as discussed on the comments you will need to set up the protobuf generator plugin on your pom.xml file. Since there are no details whatsoever of your pom file I will add the basic steps that will likely help you set yours up.

Make sure you have the dependencies for the protobuf java in your <dependencies> section: from the documentation

<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-netty-shaded</artifactId>
  <version>1.66.0</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-protobuf</artifactId>
  <version>1.66.0</version>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-stub</artifactId>
  <version>1.66.0</version>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
  <groupId>org.apache.tomcat</groupId>
  <artifactId>annotations-api</artifactId>
  <version>6.0.53</version>
  <scope>provided</scope>
</dependency>

Then in the build section add the plugin for the protobuf, this code is directly from the plugin documentation I just added the path of your proto files to the configuration

<build>
  <extensions>
    <extension>
      <groupId>kr.motd.maven</groupId>
      <artifactId>os-maven-plugin</artifactId>
      <version>1.7.1</version>
    </extension>
  </extensions>
  <plugins>
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>0.6.1</version>
      <configuration>
        <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
        <protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.66.0:exe:${os.detected.classifier}</pluginArtifact>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>compile-custom</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

I would recommend looking through the documentation for any specifics.

3

So if you’d like it to play nice, you have to generate the files while you’re working.

From your resources folder in terminal (which you can do from a terminal inside intellij), you will need to run the command to generate the java code from the proto:

protoc --java_out="../java" --proto_path=./ *.proto

Note: You may want to set up a script that removes any proto files, and then recreates the protos if you’re going to be making additions/changes to the proto as time goes on. It’s easier than doing it manually everytime.

Since it’s inside the same project, you should be able to run a mvn clean install to get it to try to make the connections for you.

If it does not automatically add the source files, you can add them by going into your settings -> modules -> sources (I believe), and adding the source folder.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Getting IntelliJ to generate Java Sources from Proto files

On my Mac I have libprotoc 27.3 (Protocol Buffers (Protobuf)) installed locally and I am trying to get it to “play nice” with my Java 17 app inside of IntelliJ. My Java app has the following high-level directory structure:

myapp/
    src/
        main/
            java/
            proto/
            resources/

Inside the src/main/proto/ directory is a single file called dostuff.proto and it looks like this:

syntax = "proto3";

package stuffdoer;
option java_multiple_files = true;
option java_package = "com.me.myapp.dostuff.stuffdoer";

message DoStuffRequest{
  bytes image = 1;
}

message DoStuffResponse{
  string stuff_image = 1;
}

service DoStuffDetection{
  rpc GetStuffToDo(DoStuffRequest) returns (DoStuffResponse) {}
}

I believe the idea is that I need to run this tool to generate Java source code that can then be used in other areas of this project. Sure enough, I see that there are Java source files that seem to expect Java code generated by this file. One example is in a Java source file called DoStuffClient that declares the following import statements:

package com.me.myapp.dostuff.stuffdoer.client;

import com.google.protobuf.ByteString;
import com.me.myapp.dostuff.stuffdoer.DoStuffGrpc;
import com.me.myapp.dostuff.stuffdoer.DoStuffRequest;
import io.grpc.StatusRuntimeException;

...

In IntelliJ, I am getting compiler errors because I have not explicitly ran Google Protocol Buffers to read this dostuff.proto and generate the DoStuffGrpc and DoStuffRequest Java sources.

When I run the Maven build locally and run the app on my local machine, it starts up and I am able to manually test the code in the DoStuffClient and verify it works just fine. So I believe Maven is correctly generating the Java sources (defined in dostuff.proto) and making them available to the Java runtime.

However it is annoying to be getting the compiler errors in IntelliJ. What can I do to invoke Google Protocol Buffers from inside IntelliJ so that it generates the Java sources (defined in dostuff.proto) and makes them available to the compile-time path, so that I stop seeing compiler errors when I’m writing code in IntelliJ?

What are the exact steps to take to get IntelliJ generating Java sources from my .proto files and then getting it to link them to the compile-time path so that other source files in the opened project can “see” them?

4

So as discussed on the comments you will need to set up the protobuf generator plugin on your pom.xml file. Since there are no details whatsoever of your pom file I will add the basic steps that will likely help you set yours up.

Make sure you have the dependencies for the protobuf java in your <dependencies> section: from the documentation

<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-netty-shaded</artifactId>
  <version>1.66.0</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-protobuf</artifactId>
  <version>1.66.0</version>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-stub</artifactId>
  <version>1.66.0</version>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
  <groupId>org.apache.tomcat</groupId>
  <artifactId>annotations-api</artifactId>
  <version>6.0.53</version>
  <scope>provided</scope>
</dependency>

Then in the build section add the plugin for the protobuf, this code is directly from the plugin documentation I just added the path of your proto files to the configuration

<build>
  <extensions>
    <extension>
      <groupId>kr.motd.maven</groupId>
      <artifactId>os-maven-plugin</artifactId>
      <version>1.7.1</version>
    </extension>
  </extensions>
  <plugins>
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>0.6.1</version>
      <configuration>
        <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
        <protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.66.0:exe:${os.detected.classifier}</pluginArtifact>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>compile-custom</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

I would recommend looking through the documentation for any specifics.

3

So if you’d like it to play nice, you have to generate the files while you’re working.

From your resources folder in terminal (which you can do from a terminal inside intellij), you will need to run the command to generate the java code from the proto:

protoc --java_out="../java" --proto_path=./ *.proto

Note: You may want to set up a script that removes any proto files, and then recreates the protos if you’re going to be making additions/changes to the proto as time goes on. It’s easier than doing it manually everytime.

Since it’s inside the same project, you should be able to run a mvn clean install to get it to try to make the connections for you.

If it does not automatically add the source files, you can add them by going into your settings -> modules -> sources (I believe), and adding the source folder.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật