1

I have a java 8 Project with java fx. I recently switched to Java11 and OpenJFX11.

Everything runs fine with gradle run or with JVM arguments. But as soon as I build a jar, the executable jar will not execute because of this error:

Error: Could not find or load main class sample.application.main.SampleAppLauncher

Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

buildscript {
    dependencies {
        classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.7.0'
        classpath 'org.openjfx:javafx:11'
    }
    repositories {
        mavenLocal()
        mavenCentral()

    }
}

plugins {
    id 'java'
    id 'java-library'
    id 'maven-publish'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

sourceCompatibility = 11
targetCompatibility = 11
repositories {
    
    jcenter()
    mavenCentral()
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    archiveClassifier.set("sources")
}
task javadocJar(type: Jar, dependsOn: javadoc) {
     archiveClassifier.set("javadoc")
    from javadoc.destinationDir
}


mainClassName="sample.application.main.Launcher"

publishing {
     publications {
            mavenAar(MavenPublication) {
                from components.java
                afterEvaluate {
                    artifact javadocJar
                    artifact sourcesJar
                }
            }
        }
    }
    
javafx {
    version = "11"
    modules = [ 'javafx.controls', 'javafx.fxml','javafx.graphics']
}


sourceSets {
    main.java.srcDir "src/main/java"
    main.resources.srcDir "src/main/resources"
}


dependencies {
    
    api 'org.apache.commons:commons-math3:3.6.1'

   
    implementation 'com.google.guava:guava:27.0.1-jre'
    
    
    testImplementation 'junit:junit:4.12'
    implementation 'org.openjfx:javafx:11'
    compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.28.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    compile group: 'org.reflections', name: 'reflections', version: '0.9.11'
    compile group: 'org.openjfx', name: 'javafx', version: '11'
    
    
    
}

jar {
  manifest {
    attributes(
      'Main-Class': 'sample.application.main.Launcher'
    )
    
  }
  from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Do I need to define something more for a regular Jar Build ? I the only way I know right now is to run the Application with JVM arguments with the path to my openjfx-11 . But this won't work on any other system that does not have JavaFX11.

I thought it would be compiled by gradle like every other dependecy into the jar.

Is there something differend about JFX11 ?

EDIT:

Application Class:


public class SampleAppLauncher extends Application {


    @Override
    public void start(Stage stage) throws Exception {
        \\... startup
        

    }

}

The Launcher:

public class Launcher {
    public static void main(String[] args) {
        SampleAppLauncher.launch(args);
    }
}

Module info

module FXproject {
    requires javafx.controls;
    requires javafx.fxml;
    requires transitive javafx.graphics;
    
    opens sample.application.main to javafx.fxml;
    exports sample.application.main;
}

Community
  • 1
  • 1
Luxusproblem
  • 1,913
  • 11
  • 23
  • 3
    Find a detailed explanation on how to fix it [here](https://stackoverflow.com/a/52571719/3956070), with an update [here](https://stackoverflow.com/a/57452893/3956070). Anyway, it is documented in https://openjfx.io/openjfx-docs/#modular (section non-modular with Gradle). – José Pereda Oct 30 '19 at 09:12
  • 2
    You are using `configurations.compile.collect`, and as I've explained in the linked question above, you need to use `configurations.runtimeClasspath`, because of the change `compile` -> `implementation`. – José Pereda Oct 30 '19 at 10:27
  • Is your main class a subclass of `Application`? – Slaw Oct 30 '19 at 10:28
  • @JoséPereda oh thanks. Must have overread that. Ok now i get the **JavaFX runtime components are missing, and are required to run this application** exception, despite the javafx plugin and **modules = [ 'javafx.controls', 'javafx.fxml']** . Do I need to create a module-info.java ? – Luxusproblem Oct 30 '19 at 10:42
  • That is the other part of the question I also linked, and you might have _overread_ as well. Slaw also pointed it out. If your main class extends from `Application`, you need to use a "Launcher" class. – José Pereda Oct 30 '19 at 10:43
  • @JoséPereda I also tried it with a launcher but I get: **class sample.application.main.Launcher is not a subclass of javafx.application.Application** and the other way arround it crashes because the modules are missing (as expected) . I also tried Jlink and "created a module-info.java" but somehow it won't work. – Luxusproblem Oct 30 '19 at 10:59
  • Launcher shouldn't extend from Application. Edit your question and post it. – José Pereda Oct 30 '19 at 11:03
  • @JoséPereda I edited my question. I added the launcher, the App class and the module Info – Luxusproblem Oct 30 '19 at 11:09
  • 1
    You need to use `Application.launch(SampleAppLauncher.class, args)`. – Slaw Oct 30 '19 at 11:11
  • 1
    Either that, or `SampleAppLauncher.main(args)` – José Pereda Oct 30 '19 at 11:11

0 Answers0