Java Maven编译“找不到符号”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29970729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:54:42  来源:igfitidea点击:

Maven compile "Cannot find symbol"

javamavencodeship

提问by quarks

For some reason this command works fine with my local machine:

出于某种原因,这个命令在我的本地机器上运行良好:

mvn clean install -DskipTests=true -Psdk

However for Codeship it does now work and throws this "Cannot find symbol" error. In Codeship the full command is:

但是,对于 Codeship,它现在可以工作并抛出此“找不到符号”错误。在 Codeship 中,完整的命令是:

jdk_switcher use oraclejdk8
echo $JAVA_HOME
mvn clean install -DskipTests=true -Psdk

In the POM the repository have this:

在 POM 中,存储库具有以下内容:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

Error:

错误:

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ client-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 65 source files to /home/rof/src/bitbucket.org/company/client-app/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/rof/src/bitbucket.org/company/client-app/src/main/java/com/client/rest/resources/MyResource.java:[3,61] cannot find symbol
symbol:   class MyEntity

采纳答案by Shubham Jain

Your Maven is reading Java version as 1.8, Where as the pom.xml says the version is 1.7.

您的 Maven 将 Java 版本读取为 1.8,而 pom.xml 表示版本为 1.7。

Try installing the required verison.

尝试安装所需的版本。

If already installed check your $JAVA_HOME environment variable, it should contain the path of Java JDK 8. If you dont find it, fix your environment variable.

如果已经安装,请检查您的 $JAVA_HOME 环境变量,它应该包含 Java JDK 8 的路径。如果找不到,请修复您的环境变量。

also remove the lines

也删除线

 <fork>true</fork>
     <executable>${JAVA_1_8_HOME}/bin/javac</executable>

from the pom.xml

从 pom.xml

回答by user4906240

I had the same issue. reason was one of the class was used in

我遇到过同样的问题。原因是其中一个类被用于

src/main/java/

Where as in actual it was present in

实际上它出现在什么地方

src/test/java

回答by Pritom

If you have correctly imported dependency the you can try the following process.
First try-
mvn clean
if that runs successfully then try-
mvn install
If this works fine then you can try-
mvn compile
This process worked for me.

如果您已正确导入依赖项,则可以尝试以下过程。
首先尝试 -
mvn clean
如果运行成功,然后尝试 -
mvn install
如果这工作正常,那么你可以尝试 -
mvn compile
这个过程对我有用。

回答by Pranay Prusti

I had the similar issue but when I changed Testng version in POM it resolved the issue.

我有类似的问题,但是当我在 POM 中更改 Testng 版本时,它解决了这个问题。

<artifactId>testng</artifactId>
<version>6.9.8</version>       // Changed the version to 6.9.8
<scope>test</scope>