英文:
ClassNotFoundException error when trying to run java project using maven
问题
为了使用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文件,其中包含主函数:
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>
英文:
In order to execute a java project using maven, I put on the terminal this two commands:
To build project:
mvn package
To run project:
mvn exec:java
The build always execute with success, but every time I try to run the project, I receive this error:
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)
Do you know what may be happening?
This is my Application.java file, with main function inside
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();
}
}
This is my 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>
答案1
得分: 0
我最终完成了这个工作。我发现我需要将main / java放在src的右边,放在我的项目文件夹结构中,遵循Maven使用的模式。我没有注意到,即使我的构建命令正常工作,我的.jar文件也是空的。
英文:
I finally did this works. I discover that I need to put main / java right after src, on my project folder structure, following the pattern that maven uses. I did not notice, but even that my build command was working, my .jar file was been generated empty.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论