JavaFX + Gradle-构建找不到资源

时间:2018-11-10 22:34:43

标签: java gradle javafx

我正在为Gradle使用FibreFox JavaFX插件,当我构建并尝试调用Launcher类时,它会出现此错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:945)
 Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
 Caused by: java.lang.NullPointerException: inputStream is null.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2480)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2450)
    at team.puffin.agileproject.SceneLoader.loadScene(SceneLoader.java:24)
    at team.puffin.agileproject.Launcher.start(Launcher.java:27)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
 Exception running application team.puffin.agileproject.Launcher

相关代码为:

build.gradle

    group 'puffin'
    version '1.0-SNAPSHOT'

    apply plugin: 'java'
    apply plugin: 'javafx-gradle-plugin'

    sourceCompatibility = 1.8

    sourceSets.main {
        java {
            srcDir 'src/main/java' //assume that your source codes are inside this path
        }
        resources {
            srcDirs = ['src/main/java', 'src/main/resources']
            exclude "**/*.java"
        }
    }

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

    repositories {
        mavenCentral()
        mavenLocal()
    }

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

    jfx {
      verbose = true;
      jfxAppOutputDir = "build/jfx/app"
      jfxMainAppJarName = "project-jfx.jar"
      // minimal requirement for jfxJar-task
      mainClass = 'team.puffin.agileproject.Launcher'

      // minimal requirement for jfxNative-task
      vendor = 'TeamPuffin'

      // gradle jfxJar
      css2bin = false
      preLoader = null // String
      updateExistingJar = false
      allPermissions = false
      manifestAttributes = null // Map<String, String>
      addPackagerJar = true
      copyAdditionalAppResourcesToJar = false
      skipCopyingDependencies = false
      useLibFolderContentForManifestClasspath = false
      fixedManifestClasspath = null
    }

Launcher.java启动方法

    public void start(Stage primaryStage) throws Exception {
        // Setup stage properties
        primaryStage.setWidth(WIDTH);
        primaryStage.setHeight(HEIGHT);
        primaryStage.setResizable(false);
        centerScreen(primaryStage);

        // Loading initial scene
        sceneLoader = new SceneLoader(primaryStage);
        NodeControllerPair nodeController = sceneLoader.loadScene("home.fxml");
        Scene scene = new Scene((Parent) nodeController.getNode());
        primaryStage.setScene(scene);

        // Display stage
        primaryStage.show();
    } 

场景加载器方法

    public NodeControllerPair loadScene(String name) {
        FXMLLoader loader = new FXMLLoader();
        Node node = null;
        Object controller = null;
        try {
            node = loader.load(getClass().getResourceAsStream(name));
            controller = loader.getController();
        } catch (IOException e) {
            e.printStackTrace();
        }
        NodeControllerPair nodeControllerPair = new NodeControllerPair(node, controller);
        return nodeControllerPair;
    }

我也在使用IntelliJ并将其设置为Gradle项目,但是我不认为它是一个整体,因为它正在生成out文件夹。但是,它确实可以在IntelliJ中运行,但是对于该项目状态的要求,我必须使用Gradle。

我查看了有关此问题的其他帖子,并尝试了解决方案,但是它们不起作用。

Unresolved external when using boost log

0 个答案:

没有答案