JavaFX Error : Could not find or load main class home.main with gradle (IntelliJ) when I try to run my jar file

Viewed 538

I generate with gradle a jar file to test my program but when I run the command :

Java -jar file_name .jar 

I have always the same error:

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

build.gradle :

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.jfoenix:jfoenix:9.0.8'
}

javafx {
    version = "14"
    modules = [ 'javafx.controls', 'javafx.fxml']
}


jar {
    manifest {
        attributes(
                'Class-Path': configurations.compile.files.collect {"$it.name"}.join(' '),
                'Main-Class': 'home.Main')}
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Architecture

Artifacts settings

1 Answers

Update:

For making a "fatJar" this task in my build.gradle works for me:

task fatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': "home.Launcher"
    }
    baseName = 'Desired_fat_jar_name'
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    with jar
}

So you run gradlew fatJar to make the all in one jar.

It looks pretty close to what you have so try changing yours to this or try removing the "Class-Path" part from your build.gradle.


In order to launch a non-modular JavaFX application with Java 9+, you need to create a file Launcher.java with the contents something like:

package home;

/**
 * Class to launch the JavaFX application.
 *
 * This launcher class will allow launching a non-modular 9+ JavaFX application.
 */
public class Launcher
{
    /**
     * Main function executed by Java application
     * @param args
     */
    public static void main(String[] args)
    {
        Main.main(args);
    }
}

Where "Main.main" is the main function in your class that extends the JavaFX Application class.

You will also need to change the main class in your build.gradle to 'home.Launcher'.

Related