-1

It's my first time trying out maven and I can't understand why I keep getting classnotfoundexception every time I am trying to build. This is the error I am receiving:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------< owmapi:owmapi >----------------------------
[INFO] Building OwmAPICase 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ owmapi ---
[WARNING] 
java.lang.ClassNotFoundException: owmapi.Main
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:246)
    at java.base/java.lang.Thread.run(Thread.java:832)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.582 s
[INFO] Finished at: 2021-02-28T14:09:17+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default-cli) on project owmapi: An exception occured while executing the Java class. owmapi.Main -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

And this is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>owmapi</groupId>  <!-- case.se.ctk -->
    <artifactId>owmapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OwmAPICase</name>

     <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties> 

    <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <mainClass>owmapi.Main</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.aksingh/owm-japis -->
        <dependency>
            <groupId>net.aksingh</groupId>
            <artifactId>owm-japis</artifactId>
            <version>2.5.3.0</version>
        </dependency>
    </dependencies>
</project>

Everything works fine if i simply run the project as a java application in eclipse but for some reason it won't work with maven. I guess the problem lies in mainClass in my pom but I've tried several solutions to no avail.

  • Can you the whole stack trace rather than a few lines. It helps on tracing the real issue. – Kidus Tekeste Feb 28 '21 at 13:30
  • @KidusTekeste Sorry thought that everything wasn't needed. I have edited my question now. – Anton Andersson Feb 28 '21 at 13:34
  • Is the package for the `Main` class defined correctly with all the packages in the pom file? inside `` of the pom configuration? – Kidus Tekeste Feb 28 '21 at 13:35
  • or simply is `owmapi.Main` your correct package name? if you have left some other package names please specify the full package name. Just incase you haven't added the full package. – Kidus Tekeste Feb 28 '21 at 13:37
  • @KidusTekeste I thought it was defined correctly. Isn't it groupid.Main? Therefore, owmapi.Main in my case. I may be wrong since I've never used maven before. – Anton Andersson Feb 28 '21 at 13:39
  • @KidusTekeste I mean the only package im using is OWM japi, do i need to include that in the mainclass? This is only a test project so there's only one .java file – Anton Andersson Feb 28 '21 at 13:42
  • Yes please, add the full package name. That's how it how the maven config will get the class name. Try it with full package name you have. – Kidus Tekeste Feb 28 '21 at 13:47
  • In my case I have a long package name and its: ``` default-cli com.miu.edu.cs.mpp.project.AttendanceManagementSystem.ui.generateattendance.AttendanceGenerate ``` – Kidus Tekeste Feb 28 '21 at 13:51
  • 1
    @KidusTekeste Sorry I've fixed it now, I didn't realize that "Main" meant the name of the class. So stupid – Anton Andersson Feb 28 '21 at 14:05
  • Glad it worked for you. – Kidus Tekeste Feb 28 '21 at 14:05

1 Answers1

0

I think your main class have some dependencies on the jar mentioned in the pom.xml. You're simply creating a target jar which doesn't have those dependencies included. You need to create the uber/fat jar which include all the relevant dependencies. You can use this following plugin maven-assembly-plugin for creating the target jar.

Assumption: Main.java class is under package owmapi.

<build>
    <finalName>owmapi</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>owmapi.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>
b.s
  • 2,409
  • 2
  • 16
  • 26
  • Alright so I tried mvn clean install and then tried to run the jar file but I get the same error, classnotfoundexception. – Anton Andersson Feb 28 '21 at 13:49
  • Make sure that `Main.java` class should be present in the `package owmapi`. – b.s Feb 28 '21 at 13:51
  • So sorry I'm apparently retarded since I didn't realize that "Main" meant that was what the class was called. It works now after I changed that. Thanks – Anton Andersson Feb 28 '21 at 14:03