Scala getClass.getResource() returning null

后端 未结 4 1624
孤城傲影
孤城傲影 2021-01-01 11:10

I have this code:

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

and it always returns null (or Unit

4条回答
  •  既然无缘
    2021-01-01 11:11

    You have three options:

    • take advantage of relative path to your current package (where Test.class is):

      getClass.getResource("test.fxml")
      
    • you can use absolute path:

      getClass.getResource("/com/mysite/main/test.fxml")
      
    • or load through the ClassLoader (note that it always start from root):

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

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

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

提交回复
热议问题