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().getResourc
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
Hope this helps. Also, details tutorial.
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~
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>()
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.
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
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.