Issue Description:
I am developing a JavaFX application using IntelliJ IDEA and encountering an issue where the application does not locate a CSS file placed in the resources folder.
In my project, there’s a style.css
file located under src/main/resources/view
. Despite this, when attempting to load it in my JavaFX application with getClass().getResource("/view/style.css")
, it consistently returns null
.
Relevant Code Implementation:
public class MainGui extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
borderPane.setCenter(new DocumentSelectionView());
Scene scene = new Scene(borderPane, 1400, 900);
// Attempting to load the CSS file
scene.getStylesheets().clear();
System.out.println(System.getProperty("user.dir")); // Prints the project directory
// Different methods tried to add the stylesheet
// Method 1
scene.getStylesheets().add("src/main/resources/view/style.css");
// Method 2
scene.getStylesheets().add(getClass().getResource("/view/style.css").toExternalForm());
}
}
Problems Encountered:
-
Method 1 (
scene.getStylesheets().add("src/main/resources/view/style.css");
):- Results in the warning:
WARNING: Resource "src/main/resources/view/style.css" not found.
- Results in the warning:
-
Method 2 (
scene.getStylesheets().add(getClass().getResource("/view/style.css").toExternalForm());
):- Returns
null
, leading to an error when attempting to call.toExternalForm()
.
- Returns
Additional Information:
-
The project directory is correctly printed, confirming the working directory.
-
I have verified that there are no typographical errors in the file path.
-
The CSS file certainly exists in the specified directory.
-
The CSS file is not in the target output folder.
-
I am using IntelliJ IDEA for my project structure, not Maven or Gradle. Here is the relevant content from my
.iml
file:<?xml version="1.0" encoding="UTF-8"?> <module version="4"> <component name="AdditionalModuleElements"> <content url="file://$MODULE_DIR$" dumb="true"> <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> <excludeFolder url="file://$MODULE_DIR$/target" /> </content> </component> </module>
Question:
What could be causing this failure to locate the style.css
file, and how can I correctly load it into my JavaFX application?
Any insights or suggestions would be greatly appreciated!
Attempts:
I have attempted several approaches to resolve the issue where getResource()
consistently returns null
. Here’s a summary of the steps I’ve taken:
-
Replicated the issue in other classes across different packages, encountering the same problem with
getResource()
returningnull
. -
Tried various strings to locate the CSS file, including:
-
"style.css"
-
"/style.css"
-
"view/style.css"
-
"/view/style.css"
-
-
Moved the CSS file directly into the
resources
folder. -
Experimented with different CSS file names and methods of creating these files.
-
Restarted IntelliJ IDEA.
-
Rebuilt the entire software.
-
Invalidated the cache in IntelliJ IDEA and performed a restart (I do this after every change just to be sure).
-
I did try replicating this in other classes of other packages. The getRecource() always returns null.
-
To Settings -> Compiler -> Resource patterns I’ve added
!?*.css
-
getClass().getClassLoader().getResource("/view/style.css");
-
Moving the resource folder directly in the project folder /src/main/resources -> /resources
2
Answers
Thank you for the responses and the help provided!
After reviewing your feedback, I got confirmation that the problem wasn't with the code itself but rather with the project setup in IntelliJ. The exact cause within the setup remained elusive, but I found a solution by taking a different approach.
I decided to rebuild the project using Maven. A key part of this process was correctly setting up the VM arguments for JavaFX, which was initially challenging due to the scattered dependencies. Once these were properly configured, the application successfully recognized and loaded the CSS file.
Reconstructing the project in Maven proved to be the effective solution I needed. Thank you all for your assistance.
For those having the same problem and want to use maven aswell here are my setup files:
pom.xml
4.0.0
VM argument to be able to run javafx
--module-path /home/guillaume/.m2/repository/org/openjfx/javafx-base/21.0.1/:/home/guillaume/.m2/repository/org/openjfx/javafx-controls/21.0.1/:/home/guillaume/.m2/repository/org/openjfx/javafx-fxml/21.0.1/:/home/guillaume/.m2/repository/org/openjfx/javafx-graphics/21.0.1/ --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics
You need to add : between all your javafx folders so that all correct jars are selected.
You don’t want to do that.
That treats
.css
files as NOT resources. That is the opposite of what you want.Note though that (I think) the compiler setting is just for handling files under directories marked as
Sources
, and won’t affect files that are marked asResources
. From my tests,.css
files under directories I marked aResources
were always copied to the output directory, regardless of theCompiler | Resource patterns
setting. So it likely isn’t the root cause of your issue.Working example
I tried:
By default (with no other change) it the
.css
file I put in the resource directory I created was copied to the build output directory and could be found by agetResource
lookup as expected.After building:
Contained these files:
/src/Main.java
/resources/styles.css
Output
Settings | Compile | Resource patterns
These are defaults for a new Java project with Idea 2023.2.4 (UE), I didn’t change them:
Recommendation
Using a build tool (e.g. Maven or Gradle), following the Maven standard layout, and synching the build project with your IDE is usually a better approach than manually configuring the project in the IDE.