Java IntelliJ IDEA - getClass().getResource("...") 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26328040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ IDEA - getClass().getResource("...") return null
提问by BilalDja
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.
我使用的是 IntelliJ IDEA 13.1.5,我曾经使用过 Eclipse。我正在开发 JavaFX 应用程序,我尝试使用 getClass().getResource() 在我的 MainApp 类中加载 FXML 文件。我阅读了文档并尝试了几个想法,最后我有null。
This is the hierarchy :
这是层次结构:
dz.bilaldjago.homekode.MainApp.java
dz.bilaldjago.homekode.MainApp.java
dz.bilaldjago.homekode.view.RootLayout.FXML
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
我尝试了其他解决方案,例如从根提供 url 并使用 classLoader
the result is the same. Any idea please
结果是一样的。任何想法请
回答by stacky
For those who use Intellij Idea: check for Settings -> Compiler -> Resource patterns
.
对于那些使用 Intellij Idea 的人:检查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.
该设置包含应解释为资源的所有扩展。如果扩展不符合此处的任何模式,则 class.getResource 将为使用此扩展的资源返回 null。
回答by MewX
I solved this problem by pointing out the resource root
on IDEA.
我通过指出resource root
on IDEA解决了这个问题。
Right click
on a directory (or just the project name) -> Mark directory As
-> Resource Root
.
Right click
在目录(或只是项目名称)上 -> Mark directory As
-> Resource Root
。
Recompile & rejoice :P Hope this working for you~
重新编译并高兴 :P 希望这对你有用~
回答by Metin Dagcilar
I gave up trying to use
getClass().getResource("BookingForm.css"));
我放弃尝试使用
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");
相反,我创建了一个 File 对象,从该对象创建了一个 URL,然后将其传递给 getStyleSheets() 或 setLocation()File file = new File(System.getProperty("user.dir").toString() + "/src/main/resources/BookingForm.css");
scene.getStylesheets().add(folder.toURI().toURL().toExternalForm());
scene.getStylesheets().add(folder.toURI().toURL().toExternalForm());
回答by Joop Eggen
Windows is case-sensitive, the rest of the world not. Also an executable java jar (zip format) the resource names are case sensitive.
Windows 区分大小写,世界其他地方不区分大小写。也是一个可执行的 java jar(zip 格式),资源名称区分大小写。
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.
还要编译成jar,并检查fxml文件是否已添加到jar(zip文件)中。如果不是 IntelliJ 资源路径由其他答案处理。
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:
顺便说一下,这是相对于getClass()
. 请注意,如果您扩展此类,完整路径会发生变化,最好使用:
MainApp.class.getResource("view/RootLayout.fxml")
回答by Max Yan
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
如果您的项目是 maven 项目,请检查目标代码以查看您的 .fxml 文件是否存在。如果它不存在,只需添加
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
in your pom.xml
在你的 pom.xml
回答by AlwaysLearner
TL; DR;
TL; 博士;
Put your resources in
resources
folder.Use them with one slash before their names:
getClass().getResource("/myfont.ttf");
将您的资源放在
resources
文件夹中。在它们的名字前加上一个斜线使用它们:
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.
如果您正在使用Intellij IDEA
并创建了一个Maven
项目,您应该将您的资源放在resources
文件夹中(由 intellij 本身标记为资源根目录),并将这些资源放在您编译的应用程序的根目录中。
I mean, /resources/myfont.ttf
will go to /myfont.ttf
in the resulting build.
我的意思是,/resources/myfont.ttf
将转到/myfont.ttf
生成的构建中。
So you should get it via /myfont.ttf
and not myfont.ttf
. Use it like this:
所以你应该通过/myfont.ttf
而不是myfont.ttf
. 像这样使用它:
getClass().getResource("/myfont.ttf");
No need to change anything else. Just this one helped me.
无需更改任何其他内容。只是这个帮助了我。
回答by App Shah
As per suggestion, updated answer.
根据建议,更新了答案。
Step-1
第1步
- Right click on project
- Click on Mark Directory as
- Click on Sources Root
- 右键单击项目
- 单击将目录标记为
- 单击源根
Step-2
第2步
- Click on File in Menu Bar
- Click on Project Structure… to open settings panel
- 单击菜单栏中的文件
- 单击项目结构...打开设置面板
Step-3
步骤 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.
重新运行你的 java 程序,现在它应该可以工作了。
<---Previous Answer---->
<---上一个答案---->
Fixed similar issue today by adding resources folder into Resource Tab in IntelliJ IDE
通过将资源文件夹添加到 IntelliJ IDE 的资源选项卡中,今天修复了类似问题
Hope this helps. Also, details tutorial.
希望这可以帮助。另外,详细教程。
回答by kinORnirvana
If your project is Gradle, check Settings
-> Build, Execution, Deployment
-> Gradle
-> Build and run
section.
Ensure that "Build and run using"
option is "Gradle"
.
如果您的项目是 Gradle,请检查Settings
-> Build, Execution, Deployment
-> Gradle
->Build and run
部分。确保该"Build and run using"
选项为"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".
Intellij IDEA 的编译器不会复制“资源”来构建目录或测试类路径。此外,它意外地使用了虚拟的“out”目录而不是“build”。
UPDATED: only 1 option "Build and run using" is enough to set to Gradle.
更新:只有 1 个选项“Build and run using”足以设置为 Gradle。