gpt4 book ai didi

java - Maven错误: cannot find symbol class in a package

转载 作者:行者123 更新时间:2023-11-30 06:12:08 25 4
gpt4 key购买 nike

这是我第一次使用 Maven,我试图了解它是如何工作的。因此,我有一个小示例,其中两个类位于不同的包中。

package maven.callee ;

public class Callee {
public void callMe() {
System.out.println("Callee says: You called me!");
}
}

现在是带有 main 函数的 Caller 类。

package maven.caller ;
import maven.callee.Callee ;

public class Caller {
public static void main(String [] args) {
Callee callee = new Callee ();
callee.callMe ();
}
}

我知道src目录的结构与标准不一样。当我在 pom.xml 文件上运行命令 mvn package 时,我得到以下输出:

[INFO] Compiling 1 source file to /home/ced/workspaceForOxygene/build/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[2,20] package maven.callee does not exist
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,4] cannot find symbol
symbol: class Callee
location: class maven.caller.Caller
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,24] cannot find symbol
symbol: class Callee
location: class maven.caller.Caller
[INFO] 3 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.645 s
[INFO] Finished at: 2018-04-24T17:12:36+02:00
[INFO] Final Memory: 13M/158M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project mavenTest: Compilation failure: Compilation failure:
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[2,20] package maven.callee does not exist
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,4] cannot find symbol
[ERROR] symbol: class Callee
[ERROR] location: class maven.caller.Caller
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,24] cannot find symbol
[ERROR] symbol: class Callee
[ERROR] location: class maven.caller.Caller
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

请问发生什么事了?为什么maven找不到该文件? hier 是我的 POM 文件,我做错了什么?

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.swt.build</groupId>
<artifactId>mavenTest</artifactId>
<version>1.0</version>

<build>
<sourceDirectory>src/maven/caller</sourceDirectory>
<!--specify output directory for binaries -->
<outputDirectory> classes </outputDirectory>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins </groupId>
<artifactId> maven-jar-plugin</artifactId>
<version> 3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath> true </addClasspath>
<mainClass> maven.caller.Caller </mainClass>
</manifest>
</archive>
<!--specify output directory for jar file -->
<outputDirectory>jars</outputDirectory>
</configuration>
</plugin>
</plugins>

</build>
</project>

最佳答案

编译器路径中缺少 maven.callee。它应该是源目录的一部分或者应该定义为 lib 依赖项。在当前设置下,Maven 将尝试仅编译 src/maven/caller 下的 java 文件。将 pom.xml 中的 sourceDirectory 更改为 src/maven。

关于java - Maven错误: cannot find symbol class in a package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50005355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com