【问题标题】:ClassNotFoundException error when trying to run java project using maven尝试使用 maven 运行 java 项目时出现 ClassNotFoundException 错误
【发布时间】: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 我已经阅读了这篇文章,我按照他说的做了,但它对我的情况不起作用。

标签: java maven


【解决方案1】:

我终于完成了这项工作。我发现我需要在 src 之后将 main / java 放在我的项目文件夹结构中,遵循 maven 使用的模式。我没有注意到,但即使我的构建命令正在运行,我的 .jar 文件也被生成为空的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-05
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2014-06-04
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多