Concern Description:
While developing a JavaFX application using IntelliJ IDEA, I'm facing an issue where the application cannot find a CSS file placed in the resources folder.
In my project, there's a style.css
file located under src/main/resources/view
. However, when attempting to load it in my JavaFX application with
, it keeps returning getClass().getResource("/view/style.css")
null
.
Code Implementation of Note:
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);
// Trying to load the CSS file
scene.getStylesheets().clear();
System.out.println(System.getProperty("user.dir")); // Displays the project directory
// Various attempts tried to add the stylesheet
// Approach 1
scene.getStylesheets().add("src/main/resources/view/style.css");
// Approach 2
scene.getStylesheets().add(getClass().getResource("/view/style.css").toExternalForm());
}
}
Hurdles Encountered:
Approach 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:
Approach 2 (
):scene.getStylesheets().add(getClass().getResource("/view/style.css").toExternalForm());
- Returns
null
, leading to an error upon calling.toExternalForm()
.
- Returns
Additional Insights:
The project directory is accurately displayed, confirming the current working directory.
I have double-checked for any typographical errors in the file path.
The CSS file undeniably exists in the specified directory.
The CSS file is not in the output target folder.
I am utilizing IntelliJ IDEA for my project setup, without Maven or Gradle. Here's the pertinent content from my
.iml
document:<?xml version="1.0" encoding="UTF-8"?gt; <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>
Query:
What could be the reason behind this failure to locate the style.css
file, and how can I correctly incorporate it into my JavaFX application?
Your insights and suggestions are highly valued!
Efforts Made:
I have made numerous attempts to solve the persisting issue where getResource()
continuously outputs null
. Below are the steps I've taken:
Duplicated the problem in other classes within different packages, encountering the same dilemma of
getResource()
returningnull
.Tried different strings to access the CSS file, such as:
"style.css"
"/style.css"
"view/style.css"
"/view/style.css"
Moved the CSS file directly to the
resources
folder.Experimented with various CSS file names and creation methods.
Restarted IntelliJ IDEA.
Rebuilt the entire software.
Invalidated the cache in IntelliJ IDEA and restarted (I do this after every alteration just to make sure).
I did attempt replicating this in other classes of different packages. The getRecource() consistently returns null.
Added
!?*.css
to Settings -> Compiler -> Resource patterns.getClass().getClassLoader().getResource("/view/style.css");
Moved the resource folder directly into the project folder /src/main/resources -> /resources