IntelliJ IDEA - getClass().getResource(“…”) return null

前端 未结 9 723
挽巷
挽巷 2020-12-15 16:21

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

9条回答
  •  不知归路
    2020-12-15 16:55

    TL; DR;

    1. Put your resources in resources folder.

    2. 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.

提交回复
热议问题