Scala getClass.getResource()返回null [英] Scala getClass.getResource() returning null

查看:169
本文介绍了Scala getClass.getResource()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

val url: URL = getClass.getResource("com/mysite/main/test.fxml")

它总是返回 null (或单位)。我在项目中只有两个文件:

and it always returns null (or Unit). I have only two files in the project:

MyProj/src/com/mysite/main/Test.scala
MyProj/src/com/mysite/main/test.fxml

当我运行<$ c时$ c> Test.scala url 值始终为空。

and when I run the Test.scala the url value is always null.

我只是尝试重建项目,我正在使用IntelliJ IDEA。我在这里做错了什么?

I just tried rebuild the project, I am using IntelliJ IDEA. What am I doing wrong here?

推荐答案

您有三种选择:


  • 利用当前包的相对路径(其中 Test.class ):

getClass.getResource("test.fxml")


  • 你可以使用绝对路径:

  • you can use absolute path:

    getClass.getResource("/com/mysite/main/test.fxml")
    


  • 或通过 ClassLoader加载(请注意,它始终从root开始):

  • or load through the ClassLoader (note that it always start from root):

    getClass.getClassLoader.getResource("com/mysite/main/test.fxml")
    


  • 在IntelliJ中IDEA,请确保已将;?*。fxml 添加到:

    In IntelliJ IDEA, make sure you have added ;?*.fxml to the:

    设置(Mac上的偏好设置)| 编译器 | 资源模式

    Settings (Preferences on Mac) | Compiler | Resource Patterns.

    这篇关于Scala getClass.getResource()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆