Maven & Protobuf compile error: Cannot find symbol in package com.google.protobuf

32,504

Solution 1

I had the same problem. building the protobuf sources from google directly (I used 2.5.0) and doing

mvn install:install-file -Dpackaging=jar -DgeneratePom=true  -DgroupId=com.google.protobuf   -DartifactId=protobuf-java   -Dfile=protobuf-java-2.5.0.jar -Dversion=2.5.0

fixed the problem for me.

In my earlier trials I noticed, that the jar-file in /root/.m2/repository/com/google/protobuf/protobuf-java/2.5.0/ was missing.

Maybe try to use version 2.5.0 in the pom.xml and/or copying the jarfile manually.

cheers

Solution 2

I had this problem when there was a mismatch between the protoc version installed and the version listed in the pom. Matching the versions fixed the problem. In my case, I had to switch my protoc version back to 2.4.1 to match the POM.

Solution 3

The protoc --version has to be the same version to as set in pom.xml file (protobuf-java-2.5.0.jar).

Solution 4

My problem was that one unit test extended class from main folder. I solved it with:

<!-- Allow tests to call classes in main folder -->

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/test/java</source>
                    <source>src/main/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution 5

For me, it is resolved after using below in build script

<clearOutputDirectory>false</clearOutputDirectory

Complete build script

<build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.5.0.Final</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.19.0:exe:${os.detected.classifier}</pluginArtifact>
                    <clearOutputDirectory>false</clearOutputDirectory>
                    <outputDirectory>${basedir}/src/main/java/</outputDirectory>

                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Share:
32,504
Katie
Author by

Katie

Software Engineer specializing in everything web. These are a few of my favorite things: angular node.js javascript go python java c++ bash jquery katies.io

Updated on July 23, 2022

Comments

  • Katie
    Katie almost 2 years

    I'm new to Linux and Protobuf.. I need help.

    I'm trying to "mvn package" a project that contains many ".proto" files, and a pom.xml file of course...

    I'm working on Ubuntu

    =======================================

    ERROR

    When I run "mvn package", I receive this error:

    after

    ...
    Compiling 11 source files to .../target/classes
    ...
    

    I get a bunch of these errors:

    [ERROR] .../target/generated-sources/...java:[16457,30] cannot find symbol
    [ERROR] symbol  : class Parser
    [ERROR] location: package com.google.protobuf
    [ERROR] 
    [ERROR] .../target/generated-sources/...java:[17154,37] cannot find symbol
    [ERROR] symbol  : class Parser
    [ERROR] location: package com.google.protobuf
    [ERROR] 
    [ERROR] .../target/generated-sources/...java:[17165,30] cannot find symbol
    [ERROR] symbol  : class Parser
    [ERROR] location: package com.google.protobuf
    [ERROR] 
    [ERROR] .../target/generated-sources/...java:[17909,37] cannot find symbol
    [ERROR] symbol  : class Parser
    [ERROR] location: package com.google.protobuf
    [ERROR]
    

    =======================================

    POM

    Here is the pom.xml file, with groupId & artifactId taken out:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <parent>
         <groupId>*****</groupId>
         <artifactId>*****</artifactId>
         <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>*****</artifactId>
      <version>1.0-SNAPSHOT</version>
      <properties>
          <proto.cas.path>${project.basedir}/src</proto.cas.path>
          <target.gen.source.path>${project.basedir}/target/generated-sources</target.gen.source.path>
      </properties>
     <dependencies>
          <dependency>
                    <groupId>com.google.protobuf</groupId>
                    <artifactId>protobuf-java</artifactId>
                    <version>2.4.1</version>
                    <scope>compile</scope>
                </dependency>
      </dependencies>
      <build>
        <sourceDirectory>${project.basedir}/src</sourceDirectory>
            <plugins>
                <plugin>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>2.0.2</version>
                   <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                        <includes><include>**/commonapps/**</include></includes>
                    </configuration>            
                 </plugin>
                 <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>generate-sources</id>
                                <phase>generate-sources</phase>
                                <configuration>
                                    <tasks>
                                        <mkdir dir="${target.gen.source.path}" />    
                                        <path id="proto.path.files">
                                            <fileset dir="${proto.cas.path}">
                                                <include name="*.proto" />
                                            </fileset>  
                                        </path>
                                        <pathconvert pathsep=" " property="proto.files" refid="proto.path.files" />
    
                                        <exec executable="protoc">
                                             <arg value="--java_out=${target.gen.source.path}" />
                                             <arg value="--proto_path=${proto.cas.path}" />
                                                <arg line="${proto.files}" />
                                        </exec>
                                    </tasks>
                                    <sourceRoot>${target.gen.source.path}</sourceRoot>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
             </plugins>
         </build>
    </project>
    

    =======================================

    PROTOBUF INSTALLATION

    I've done

    ./configure
    make
    make check
    make install
    

    in protobuf/,

    and

    mvn test
    mvn install
    mvn package
    

    in protobuf/java.

    I took the jar in protobuf/java/target

    and added it to my maven repo by running:

    mvn install:install-file -Dpackaging=jar -DgeneratePom=true  -DgroupId=com.google.protobuf   -DartifactId=protobuf-java   -Dfile=protobuf-java-2.4.1.jar -Dversion=2.4.1
    

    Note that I've messed around with $LD_LIBRARY_PATH. Currently when I run echo it, I get:

    /usr/local/lib/:/usr/:/usr/lib/:/usr/local/
    

    yeah.... as you can tell I don't have a clue about setting $LD_LIBRARY_PATH

    I also ran:

    apt-get install protobuf-compiler
    

    =======================================

    PROTOC INSTALLATION

    I forgot what I did to make protoc work, but when I run

    protoc --version
    

    I get

    libprotoc 2.5.0
    

    =======================================

    MY QUESTION IS SIMILAR TO:

    Problems using protobufs with java and scala

    maven compilation failure

    =======================================

    POSSIBLE RELEVANCE?

    still not find package, after 'mvn install'

    http://www.scriptol.com/programming/protocol-buffers-tutorial.php

    Can anyone help?

    =======================================

    PROGRESS

    Apparently it's a plugin failure:

    https://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project casprotobuf: Compilation failure: Compilation failure: