【发布时间】:2020-07-21 14:28:22
【问题描述】:
为了使用maven执行一个java项目,我在终端上放了这两条命令:
构建项目:
mvn package
运行项目:
mvn exec:java
构建总是成功执行,但每次我尝试运行项目时,都会收到此错误:
java.lang.ClassNotFoundException: com.pipa.api.Application
at java.net.URLClassLoader.findClass (URLClassLoader.java:471)
at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:281)
at java.lang.Thread.run (Thread.java:834)
你知道会发生什么吗?
这是我的 Application.java 文件,里面有 main 函数
package com.pipa.api;
import com.pipa.api.handlers.FetchUserPositionHandler;
import com.pipa.api.handlers.HighScoreHandler;
import com.pipa.api.handlers.ScoreRegisterHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
public class Application {
public static void main(String[] args) throws IOException {
int serverPort = 8000;
HttpServer server = HttpServer.create(new InetSocketAddress(serverPort), 0);
server.createContext("/", new FetchUserPositionHandler());
server.createContext("/highscorelist", new HighScoreHandler());
server.createContext("/score", new ScoreRegisterHandler());
server.setExecutor(null);
server.start();
}
}
这是我的 pom.xml
<groupId>com.pipa.httpserver</groupId>
<artifactId>pipa</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.pipa.api.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
【问题讨论】:
-
我的猜测是您没有遵循 Maven 目录习惯用法;您的 .class 文件未正确写入 /target 文件夹,应用程序未正确打包,或者您用于执行的任务未正确设置 CLASSPATH。您应该记住 Java 基础知识,并检查一下 Maven 正在生成什么以及如何使用它。
-
link 我想这对你有帮助
-
duffymo 你是对的,它只是一个糟糕的文件夹模式结构,与 maven 使用的不同。
-
JustTry 我已经阅读了这篇文章,我按照他说的做了,但它对我的情况不起作用。