-1

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>
Matheus Martins
  • 161
  • 1
  • 10
  • 2
    My guess is that you aren't following the Maven directory idiom; your .class files are not being written to the /target folder properly , the app isn't packaged correctly, or the task you use to execute doesn't have the CLASSPATH set properly. You should remember Java fundamentals and do some checking into what Maven is producing and how it's used. – duffymo Apr 09 '20 at 02:55
  • [link](https://stackoverflow.com/questions/17458243/class-not-found-exception-with-exec-maven-plugin-when-run-on-linux) i think this could help you – JustTry Apr 09 '20 at 03:01
  • duffymo you are right, it was just a bad folder pattern structure, different from which maven uses. – Matheus Martins Apr 09 '20 at 03:20
  • JustTry I already read this post, I did what he said, but it didn't work on my case. – Matheus Martins Apr 09 '20 at 03:21

1 Answers1

0

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.

Matheus Martins
  • 161
  • 1
  • 10