Maven/WAR ClassNotFoundException after build

Answered

EDIT - IDE fault or OS incompatibility?: Someone has since told me to use Github Codespaces and it worked. I could not replicate this success in my IntelliJ IDE, fully up-to-date, uninstalled and reinstalled. The only difference I could find was that the Codespaces OS is linux, and my computer runs windows 10.

Sample code: https://github.com/LanCorC/EmptyWebsocket\

file structure
src
   └───main
       └───java
           └───org
               └───example
                   └───Main.class

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>EmptyWebsocket</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Websocket for multiplayer -->
        <dependency>
            <groupId>org.java-websocket</groupId>
            <artifactId>Java-WebSocket</artifactId>
            <version>1.5.7</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>org.example.Main</mainClass>
                        </manifest>
                    </archive>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Steps to recreate:

1: mvn clean package to create the .war file

2: java -cp target/EmptyWebsocket-1.0-SNAPSHOT/WEB-INF/classes/:target/EmptyWebsocket-1.0-SNAPSHOT/WEB-INF/lib/* org.example.Main to try run said file.

Result in IntelliJ, but not Github Codespaces: 

Error: Could not find or load main class org.example.Main
Caused by: java.lang.ClassNotFoundException: org.example.Main

Goals:
The provided sample code is a ‘smaller’ scale where I could recreate the same issue. I ultimately want to upload a .war of my project to upload on AWS Elastic Beanstalk. But at its current state, web.stdout.log on that platform also show the aforementioned error.

0
1 comment

Hello!

Thank you for reporting this!

The command syntax is slightly different on Windows, i. e. it uses “;” instead of “:” as classpath separator.

The following command should work:

java -cp "target\EmptyWebsocket-1.0-SNAPSHOT\WEB-INF\classes;target\EmptyWebsocket-1.0-SNAPSHOT\WEB-INF\lib\*" org.example.Main
0

Please sign in to leave a comment.