Home > Back-end >  Error: Could not find or load main class Game.GUI.Start Caused by: java.lang.NoClassDefFoundError: j
Error: Could not find or load main class Game.GUI.Start Caused by: java.lang.NoClassDefFoundError: j

Time:12-03

I'm new to maven and am having issues. I have a JavaFx app but I keep getting the error in the title. I have tried many different versions of the POM but can't get it to work. Here is the POM: `

<?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>groupId</groupId>
    <artifactId>Coursework2013</artifactId>
    <version>1.0-SNAPSHOT</version>
    <url>http://maven.apache.org</url>
    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <javafx.version>19</javafx.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.9.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Snake.GUI.Start</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>javazoom</groupId>
            <artifactId>jlayer</artifactId>
            <version>1.0.1</version>
        </dependency>
    </dependencies>
</project>

and the start of Start (as I'm guessing thats related):

package Snake.GUI;

import Snake.GUI.controller.DataHandler;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.io.IOException;
public class Start extends Application {

`

I tried many different ways of telling it where main was and all that, but it didn't help. I've looked at many similar issues but their questions on stack but their solutions have not worked for me. Thanks for your time.

CodePudding user response:

Do not blindly follow any obscure recomendations. Start with the official documentation that you can find here: https://openjfx.io/openjfx-docs/#maven You do not have to copy any dependencies into some build folder.

CodePudding user response:

You adding project dependencies in classpath, but don't have this dependencies in build folder.

You need to add maven-dependency-plugin as follows:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
    <execution>
        <id>copy-dependencies</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <outputDirectory>
                ${project.build.directory}/libs
            </outputDirectory>
        </configuration>
    </execution>
</executions>

And in maven-jar-plugin add classpathPrefix parameter:

<manifest>
    <addClasspath>true</addClasspath>
    <classpathPrefix>libs/</classpathPrefix>
    <mainClass>Snake.GUI.Start</mainClass>
</manifest>
  • Related