1

我有一个带有 java fx 的 java 8 项目。我最近切换到 Java11 和 OpenJFX11。

使用 gradle run 或 JVM 参数,一切都运行良好。但是一旦我构建了一个jar,可执行jar就不会因为这个错误而执行:

错误:无法找到或加载主类 sample.application.main.SampleAppLauncher

引起: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) }
    }
}

我是否需要为常规 Jar Build 定义更多内容?我现在唯一知道的方法是运行带有 JVM 参数的应用程序,路径为我的 openjfx-11 。但这不适用于没有 JavaFX11 的任何其他系统。

我认为它会像其他所有依赖项一样由 gradle 编译到 jar 中。

JFX11 有什么不同吗?

编辑:

应用类:


public class SampleAppLauncher extends Application {


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

    }

}

启动器:

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

模块信息

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

4

0 回答 0