I'm having some trouble getting my code, which uses JavaFX, to run in a jar file. It runs fine in Intellij, but when I try to run the jar file I get:
Exception in Application start method caused by java.lang.NullPointerException
The line in question is: loginScene.getStylesheets().add(LoginUI.class.getResource("UI.css").toExternalForm());
I'm using Gradle to build my jar so I think my problem is caused by my compiled code not including the UI.css file. My css file is currently located in the same directory as my LoginUI class.
My build.gradle file looks like this:
apply plugin: 'java'
sourceCompatibility = 1.11
repositories {
mavenCentral()
}
def currentOS = org.gradle.internal.os.OperatingSystem.current()
def platform
if (currentOS.isWindows()) {
platform = 'win'
} else if (currentOS.isLinux()) {
platform = 'linux'
} else if (currentOS.isMacOsX()) {
platform = 'mac'
}
dependencies {
compile "org.openjfx:javafx-base:11:${platform}"
compile "org.openjfx:javafx-graphics:11:${platform}"
compile "org.openjfx:javafx-controls:11:${platform}"
compile "org.openjfx:javafx-fxml:11:${platform}"
compile "com.google.code.gson:gson:2.2.4"
compile "org.apache.commons:commons-lang3:3.6"
compile "org.apache.httpcomponents:httpclient:4.5.6"
}
task run(type: JavaExec) {
classpath sourceSets.main.runtimeClasspath
main = "project.Main"
}
sourceSets {
main {
resources {
srcDirs = ["src/main/java"]
includes = ["**/*.css"]
}
}
}
jar {
manifest {
attributes 'Main-Class': 'main.java.edu.ufl.activitymonitor.Main'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
compileJava {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'javafx.controls,javafx.fxml'
]
}
}
run {
doFirst {
jvmArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'javafx.controls,javafx.fxml'
]
}
}
So far I have tried: 1. Adding the following to my build.gradle file (like in this solution):
sourceSets {
main {
resources {
srcDirs = ["src/main/java"]
includes = ["**/*.css"]
}
}
}
- Moving my css files from main/java. to main/resources so my file structure looks like this: directory structure
And then replacing "UI.css" with either: "../../../../resources/UI.css", "../../../resources/UI.css", "../../../../../resources/UI.css", or "/UI.css", or just leaving it as UI.css. However, this just results in a nullpointer error in IntelliJ.
How do I fix this?
UPDATE: I also added the last two lines to my build.gradle (from this question) and it didn't work:
jar {
manifest {
attributes 'Main-Class': 'main.java.edu.ufl.activitymonitor.Main'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
from('src/main/java') {
include '**/*.css'
}
}
Update: Since I don't have a lot of time before deploying my code for an experiment, I ended up using inline css for the most important graphical elements. It's not perfect, but it does work and it is enough for my purposes.