首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getClass().getResource()仅适用于Gradle

getClass().getResource()仅适用于Gradle
EN

Stack Overflow用户
提问于 2020-01-23 03:57:49
回答 1查看 743关注 0票数 1

我正在Windows10上使用Gradle构建一个JavaFx应用程序。

代码语言:javascript
复制
src
|--- main
     |--- java
     |    |--- thomas/software/helloworld/App.java (Main class)
     |    |--- thomas/software/helloworld/MainViewController.java (extends JavaFX Application)
     |--- resources
          |--- imgs/    (some images)
          |--- layout/  (fxml files)

代码语言:javascript
复制
FXMLLoader loader = new FXMLLoader(
                    getClass().getResource("../../../layout/main.fxml").toURI().toURL());

我像这样加载图像:

代码语言:javascript
复制
path = getClass().getClassLoader().getResource("imgs/question.png").toExternalForm();

我的问题是,当我运行gradlew distZip生成的发行版时,从生成的批处理文件中运行时找不到布局文件,并抛出空指针异常。

build/libs中生成的jar文件的结构与build/distributions中的zip文件中的结构相同,如下所示:

代码语言:javascript
复制
imgs/ (images are there)
layout/ (layout file also there)
META-INF/ 
thomas/software/helloworld/ (everything there as well)

这是我的build.gradle文件:

代码语言:javascript
复制
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.1/userguide/tutorial_java_projects.html
 */

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building a CLI application.
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

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

tasks.withType(JavaExec) {
    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9099'
    }
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:28.1-jre'

    // Use JUnit Jupiter API for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'

    // Use JUnit Jupiter Engine for testing.
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
}

application {
    // Define the main class for the application.
    mainClassName = 'thomas.software.helloworld.App'
}

test {
    // Use junit platform for unit tests
    useJUnitPlatform()
}

Java版本13

Gradle版本6.1。

我遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2020-01-23 04:47:39

感谢@Slaw和@Compass。它适用于。代码的相关部分:

代码语言:javascript
复制
var instream = getClass().getClassLoader().getResourceAsStream("layout/main.fxml");
FXMLLoader loader = new FXMLLoader();
loader.setController(this);
loader.load(instream);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59867387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档