Gradle不包括FXML和图像 [英] Gradle not including FXML and images
问题描述
我一直在寻找在 build.gradle
中包含FXML和图像的正确方法,以便将它们构建到jar中。我在此处进行了尝试,但仍然无法正常工作。
I have been looking around for the proper way to include FXML and images in the build.gradle
so that they are build into the jar. I have look here and tried it but it still does not work.
抛出此错误: java.lang.NullPointerException:inputStream为null。
这是 build.gradle
( Github ):
group 'com.voidustries'
version '1.0-SNAPSHOT'
sourceSets {
main {
resources {
srcDirs = ["src/main/java/com/voidustries/poly"]
includes = ["gui/layoutForm.fxml"]
}
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.1.0'
}
}
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'
repositories {
mavenCentral()
}
dependencies {
testCompile('org.junit.jupiter:junit-jupiter-api:5.1.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.1.0')
}
task wrapper(type: Wrapper) {
description = 'Generates gradlew[.bat] scripts'
gradleVersion = '4.3.1'
}
test {
testLogging {
events "passed", "skipped", "failed"
}
}
这是GUI.java( Github ):
Here is GUI.java (Github):
FXMLLoader loader = new FXMLLoader();
FileInputStream fis = new FileInputStream(settings);
ResourceBundle resourceBundle = new PropertyResourceBundle(fis);
loader.setResources(resourceBundle);
Parent root = loader.load(getClass().getResourceAsStream("layoutForm.fxml"));
Platform.setImplicitExit(false);
stacktrace表示 loader.load(getClass()。getResourceAsStream( layoutForm.fxml));
导致错误
The stacktrace says that loader.load(getClass().getResourceAsStream("layoutForm.fxml"));
is causing the error
这是源代码树:
com
└───voidustries
└───poly
│ CustomFormatter.java
│ Main.java
│ PolyThreads.java
│ SysTray.java
│
├───gui
│ Controller.java
│ GUI.java
│ Icon.png
│ layoutForm.fxml
│
└───img
Icon.png
您会看到 layoutForm.fxml
和 Icon.png
都在那里在树上
you can see that layoutForm.fxml
and Icon.png
are both there in the tree
这是外面的树:
com
└───voidustries
└───poly
│ CustomFormatter.class
│ Main.class
│ PolyThreads.class
│ SysTray.class
│
└───gui
Controller.class
GUI.class
在外面的树上,他们都不存在,我已经花了好几个小时试图解决这个问题。任何帮助将不胜感激。
In the out tree they are both absent and I have been fiddling for hours trying to fix this issue. Any help would be greatly appreciated.
此外,如果您需要更多此处是Github存储库
Also if you need more here is the Github repository
推荐答案
您正在放置xml文件在名为 src / main / java
的文件夹中(提示:Java目录适用于Java文件)
You are putting xml files in a folder called src/main/java
(hint: java dir is for java files)
资源(例如xml,图像文件等)应该放在 src / main / resources
Resources (eg xml, image files etc) should go in src/main/resources
Maven和Gradle。如果您想更改默认值,则可以,但是我强烈建议您考虑遵循明智的约定。
This is a standard convention followed by both Maven and Gradle. If you want to change the defaults you can, but first I strongly advise you consider following the sensible conventions.
此外,有关类加载器的说明
Also, a note on classloaders
- SomeClass.class.getResource( foo.xml)将查找相对于类包的xml
- SomeClass。 class.getResource( / foo.xml)将查找相对于类路径根的xml
在您的示例中
getClass().getResourceAsStream("layoutForm.fxml")
这是在类路径 com / voidustries / poly / gui / layoutForm.fxml
因此,如果要将资源保留在原处,则需要将 src / main / java
添加到资源文件夹中,而不是 src / main / java / com / voidustries / poly
So if you want to leave your resources where they are, you will want to add src/main/java
to the resource folders, not src/main/java/com/voidustries/poly
这篇关于Gradle不包括FXML和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!