【问题标题】:Gradle JavaFX - java.lang.NullPointerException: Location is requiredGradle JavaFX - java.lang.NullPointerException:需要位置
【发布时间】:2019-03-26 10:20:22
【问题描述】:

使用 Gradle 构建程序并通过以下命令执行时会生成空指针异常(注意:如果我在 IntelliJ IDE 中单击运行,程序可以正常工作):

java -jar main-1.0.jar

我尝试了Link 中提供的解决方案,与此案例类似,但并没有解决问题。

我也尝试了Link 中提供的解决方案,但即使这样也不能解决问题。

Gradle 脚本包含我之前查询的解决方案 - Link

FXML 文件 (Sample.fxml) 位于 src/main/resources

希望面临的问题很明确,并且有任何关于如何解决此问题的指示吗?

代码

主要:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/Sample.fxml"));
        StackPane stackPane = new StackPane(root);
        Scene scene = new Scene(stackPane);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

分级:

plugins {
    id 'java'
    id 'application'
    id 'idea'
}

group 'testJavaFx'
version '1.0-SNAPSHOT'

sourceCompatibility = JavaVersion.VERSION_1_10

repositories {
    mavenCentral()
}

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/resources"]
            includes = ["**/*.fxml"]
        }
    }
}

dependencies {
    compile 'org.openjfx:javafx-base:11:win'
    compile 'org.openjfx:javafx-controls:11:win'
    compile 'org.openjfx:javafx-fxml:11:win'
    compile 'org.openjfx:javafx-graphics:11:win'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileJava {
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls',
                '--add-modules', 'javafx.fxml',
                '--add-modules', 'javafx.base',
                '--add-modules', 'javafx.graphics'
        ]
    }
}

run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls',
                '--add-modules', 'javafx.fxml',
                '--add-modules', 'javafx.base',
                '--add-modules', 'javafx.graphics'
        ]
    }
}

def jarPackage(String jarName, String className, artifactVersion) {
    if (artifactVersion == "" || artifactVersion == null) {
        artifactVersion = "1.0.0"
    }
    return tasks.create("jar${jarName}", Jar) {
        baseName = jarName
        version = artifactVersion

        String pkgName = className.substring(0, className.lastIndexOf("."))
        String pkgDir = pkgName.replaceAll("\\.", "/")
        String clazzName = className.substring( className.lastIndexOf(".") +1 )

        from(sourceSets.main.output) {
            include "$pkgDir//**"
        }

        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes "Implementation-Title": "$clazzName",
                    "Implementation-Version": "$version",
                    "Main-Class": "$pkgName.$clazzName"
        }
    }
}

artifacts {
    archives jarPackage("Main", "pkg1.Main" , "1.0")
}

【问题讨论】:

  • 您是否检查了 jar 以查看是否将 FXML 文件添加到其中?
  • 我添加了 - from(sourceSets.main.resources) { includes = ["*/.*"] } 这似乎已经解决了这个问题。不知道这是否是正确的方法。这是在构建脚本的创建任务部分添加的。

标签: java gradle javafx build.gradle executable-jar


【解决方案1】:

设法通过删除 SourceSets(位于存储库下方)并在 tasks.Create 中添加以下代码来解决此问题

from(sourceSets.main.resources) {
            includes = ["**/*.*"]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-03
    • 2017-04-16
    • 1970-01-01
    • 2016-09-25
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多