可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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~
回答2:
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.
回答3:
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
回答4:
TL; DR;
Put your resources in resources
folder.
Use them with one slash before their names: getClass().getResource("/myfont.ttf");
Long Story;
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.
回答5:
As per suggestion, updated answer.
Step-1
- Right click on project
- Click on Mark Directory as
- Click on Sources Root
Step-2
- Click on File in Menu Bar
- Click on Project Structure… to open settings panel
Step-3
- Click on Modules tab
- As you see there isn’t any resources folder added as Content Root
- We need to add resources folder into it
Step-4
- Make sure to click on resource folder
- Click on Resources tab after that
- You will see resources folder added as Resource Folders in right panel
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.
回答6:
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());
回答7:
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")