ClassNotFoundException错误,尝试使用Maven运行Java项目时发生。

huangapple go评论78阅读模式
英文:

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(&quot;/&quot;, new FetchUserPositionHandler());

        server.createContext(&quot;/highscorelist&quot;, new HighScoreHandler());

        server.createContext(&quot;/score&quot;, new ScoreRegisterHandler());

        server.setExecutor(null);
        server.start();
    }
}

This is my pom.xml

&lt;groupId&gt;com.pipa.httpserver&lt;/groupId&gt;
    &lt;artifactId&gt;pipa&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;1.4.0&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;mainClass&gt;com.pipa.api.Application&lt;/mainClass&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

答案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.

huangapple
  • 本文由 发表于 2020年4月9日 10:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61113195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定