Can't import proto file from java file Intellij id

I'm trying to import a proto file from a java file in IntelliJ IDEA.

I have a file called A.proto and a file called B.java. I try to import a class Info from the A.proto file in the B.java file like this:

import A.Info;

However, IntelliJ IDEA doesn't look to support proto files and says my class doesn't exist. I installed both plugins Protobuf Support and Protocol Buffer Editor. But it still doesn't work. Any idea?

Upvotes: 3

Views: 7525

Answers (3)

pixelsoccupied
pixelsoccupied

Reputation: 131

Incase anyone is still stuck -

I found that specifying the path to Intellij fixes the problem:

Preferences -> Languages and Frameworks -> Protocol Buffer -> Uncheck "Configure automatically"

And then add the path to the file.

It should be good for xolstice or os72!

Upvotes: 1

Tom Cools
Tom Cools

Reputation: 1148

Problem

IntelliJ recognizes protocol buffer files, but they are not Java, so the Java Compiler doesn't know what to do with them.

Solution with Maven

You can compile those protocol buffers to Java files, which is the step you are currently missing. The best way I know is to use a Maven plugin to do this.

 <plugin>
            <groupId>com.github.os72</groupId>
            <artifactId>protoc-jar-maven-plugin</artifactId>
            <version>3.11.4</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <protocVersion>3.0.0</protocVersion> <!-- 2.4.1, 2.5.0, 2.6.1, 3.0.0 -->
                        <includeDirectories>
                            <include>src/main/resources/protobuf</include>
                        </includeDirectories>
                        <inputDirectories>
                            <include>src/main/resources/protobuf/</include>
                        </inputDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>

And dependency for the Protocol Buffer classes:

<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>3.11.4</version>
</dependency>

With this plugin, Intellij will find the created Java classes, after having initially created the sources. This way, only your Protocol Buffer files need to be in Source Control. You let the plugin take care of the compilation to Java.

After creation of the Java classes, you can use them in the rest of your code. You can even view the generated Java classes in the target/generated-sources folder in your Maven Project.

Here's what the mapping between Protocol Buffers and Java looks like:

DistanceWalked.proto

package example;

message DistanceWalked {
    string userId = 1;
    double distance = 2;
}

DistanceWalkedOuterClass.DistanceWalked.java (generated)

package example;

public class DistanceWalked {
 //properties This class isn't pretty...
}

(Full code example with protocol buffers and Maven plugin can be found here: https://github.com/TomCools/protocol-buffers-example)

Link to plugin source: https://github.com/os72/protoc-jar-maven-plugin

Solution without Maven

Without Maven, you have to download the command-line compiler. Documentation on that can be found here: https://developers.google.com/protocol-buffers/docs/javatutorial#compiling-your-protocol-buffers

Upvotes: 6

Pieterjan Deconinck
Pieterjan Deconinck

Reputation: 552

The proto file is just the description of the message format. It does not contain code that can be interpreted directly in a java context. The idea of the proto file is to provide a generic, language agnostic specification of the message format.

Based on the proto file, you can generate the corresponding java code. This code can then be used and imported in your java project.

Have a look here on how to generate code from a proto file: https://developers.google.com/protocol-buffers/docs/javatutorial#compiling-your-protocol-buffers

Upvotes: 2

Related Questions