Reputation: 1092
I'm using IntelliJ IDEA 13.1.5, I used to work with Eclipse. I'm working on JavaFX application, I try to load FXML file within my MainApp class using getClass().getResource(). I read the documentation and I try several idea, at the end I have null.
This is the hierarchy :
dz.bilaldjago.homekode.MainApp.java
dz.bilaldjago.homekode.view.RootLayout.FXML
This is the code snippet I used:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/RootLayout.fxml"));
I tried other solution such giving the url from the root and using the classLoader
the result is the same. Any idea please
Upvotes: 46
Views: 49876
Reputation: 3583
My problem was that I added it to the wrong directory src
. It needs to be in src/main
Upvotes: 0
Reputation:
As per suggestion, updated answer.
Step-1
Step-2
Step-3
Step-4
Re-run your java program and now it should work.
<---Previous Answer---->
Fixed similar issue today by adding resources folder into Resource Tab in IntelliJ IDE
Upvotes: 0
Reputation: 427
I solved this issue using method getRessource
from object ClassLoaderUtils
in Apache santuario-java (that was already in my dependency).
Upvotes: 0
Reputation: 130
In my case, ClassLoader
helped
Class testClass = getClass();
URL url = testClass.getClassLoader().getResource(/fileName);
Upvotes: 1
Reputation: 1
I had this same problem and tried all of the answers to this post. I finally found an answer on a different forum that works. This was the code I put into the build section of pom.xml
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
It is important to make sure the directory section is the directory of the resource class, not the main class.
I also changed my compiler settings as was suggested in another comment. I found this answer on a post here: https://javawithus.com/en/classloadergetresource-returns-null/
Upvotes: 0
Reputation: 1
I used below code to access the file from resource folder in spring application. I used classLoader.getResourceAsStream(FileName) instead of getClass().getResourceAsStream(FileName), Hope this would help to access the file residing in resource folder.
String fileName = "./file.txt";
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
I resolved file access so thought to share with all .
Upvotes: 0
Reputation: 4192
Very frustrating. What also happens is that the directory containing the resources are sometimes created as:
└── resources
└── my.tests
├── t1.txt
└── t2.txt
instead of:
└── resources
└── my
└── tests
├── t1.txt
└── t2.txt
The solution is to just manually recreate the directory structure so that it resembles the second example.
I also recommend to to troubleshoot your tests on the command line (with mvn clean test) if you're using maven to eliminate the chances of this being an IDE issue.
Upvotes: 3
Reputation: 100
In Project Structure
> Project
you have to make sure Project compiler output:
has its value filled in. In my case it did not. Point it to the ./target
or ./bin
(whatever you have) directory in your project.
Upvotes: 0
Reputation: 12222
first, you need to set src and resource folders for IntelliJ.
see the icon near the java(blue) and resources(yellow 4 lines) folder.
right-click on java folder and select sources root
and right-click on java folder and select resource root
after that create the same package in the java folder and resource folder.
for example: packages -> org.example
├─ src
├─ main
├─ java
| └─ com
| └─ example
| └─ A.class
|
└─ resources
└─ com
└─ example
└─ fxml
└─ AFXML.fxml
in A.class you can use this.
Java
FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/AFXML.fxml"));
Parent root = loader.load();
Kotlin
val loader = FXMLLoader(javaClass.getResource("fxml/AFXML.fxml"))
val root = loader.load<Parent>()
or
val loader = FXMLLoader(A::class.java.getResource("fxml/AFXML.fxml"))
val root = loader.load<Parent>()
Upvotes: 4
Reputation: 1715
If your project is Gradle, check Settings
-> Build, Execution, Deployment
-> Gradle
-> Build and run
section.
Ensure that "Build and run using"
option is "Gradle"
.
Intellij IDEA's compiler doesn't copy "resources" to build dir nor to tests classpath. Also, it unexpectedly uses dummy "out" dir instead of "build".
UPDATED: only 1 option "Build and run using" is enough to set to Gradle.
Upvotes: 2
Reputation: 473
Put your resources in resources
folder.
Use them with one slash before their names: getClass().getResource("/myfont.ttf");
If you are using Intellij IDEA
and you created a Maven
project, you should put your resources in resources
folder (as marked as resource root by intellij itself) and these resource go to the root of your compiled app.
I mean, /resources/myfont.ttf
will go to /myfont.ttf
in the resulting build.
So you should get it via /myfont.ttf
and not myfont.ttf
. Use it like this:
getClass().getResource("/myfont.ttf");
No need to change anything else. Just this one helped me.
Upvotes: 27
Reputation: 41
if your project is a maven project, check the target code to see whether your .fxml file exist there. if it's not there ,just add
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
in your pom.xml
Upvotes: 4
Reputation: 109613
Windows is case-sensitive, the rest of the world not. Also an executable java jar (zip format) the resource names are case sensitive.
Best rename the file
view/RootLayout.FXML
to
view/RootLayout.fxml
This must be done by moving the original file away, and creating a new one.
Also compile to a jar, and check that the fxml file was added to the jar (zip file). When not IntelliJ resource paths are treated by an other answer.
By the way this is path relative to the package path of getClass()
. Be aware if you extended this class the full path changes, better use:
MainApp.class.getResource("view/RootLayout.fxml")
Upvotes: -1
Reputation: 554
I gave up trying to use
getClass().getResource("BookingForm.css"));
Instead I create a File object, create a URL from that object and then pass it into getStyleSheets() or setLocation()
File file = new File(System.getProperty("user.dir").toString() + "/src/main/resources/BookingForm.css");
scene.getStylesheets().add(folder.toURI().toURL().toExternalForm());
Upvotes: -2
Reputation: 4312
I solved this problem by pointing out the resource root
on IDEA.
Right click
on a directory (or just the project name) -> Mark directory As
-> Resource Root
.
Recompile & rejoice :P Hope this working for you~
Upvotes: 29
Reputation: 820
For those who use Intellij Idea: check for Settings -> Compiler -> Resource patterns
.
The setting contains all extensions that should be interpreted as resources. If an extension does not comply to any pattern here, class.getResource will return null for resources using this extension.
Upvotes: 26