我正在Windows10上使用Gradle构建一个JavaFx应用程序。
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)FXMLLoader loader = new FXMLLoader(
getClass().getResource("../../../layout/main.fxml").toURI().toURL());我像这样加载图像:
path = getClass().getClassLoader().getResource("imgs/question.png").toExternalForm();我的问题是,当我运行gradlew distZip生成的发行版时,从生成的批处理文件中运行时找不到布局文件,并抛出空指针异常。
build/libs中生成的jar文件的结构与build/distributions中的zip文件中的结构相同,如下所示:
imgs/ (images are there)
layout/ (layout file also there)
META-INF/
thomas/software/helloworld/ (everything there as well)这是我的build.gradle文件:
/*
* 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。
我遗漏了什么?
发布于 2020-01-23 04:47:39
感谢@Slaw和@Compass。它适用于。代码的相关部分:
var instream = getClass().getClassLoader().getResourceAsStream("layout/main.fxml");
FXMLLoader loader = new FXMLLoader();
loader.setController(this);
loader.load(instream);https://stackoverflow.com/questions/59867387
复制相似问题