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

前端 未结 9 680
挽巷
挽巷 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:34

    As per suggestion, updated answer.

    Step-1

    1. Right click on project
    2. Click on Mark Directory as
    3. Click on Sources Root

    Step-2

    1. Click on File in Menu Bar
    2. Click on Project Structure… to open settings panel

    Step-3

    1. Click on Modules tab
    2. As you see there isn’t any resources folder added as Content Root
    3. We need to add resources folder into it

    Step-4

    1. Make sure to click on resource folder
    2. Click on Resources tab after that
    3. 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.

    0 讨论(0)
  • 2020-12-15 16:45

    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~

    0 讨论(0)
  • 2020-12-15 16:46

    first, you need to set src and resource folders for IntelliJ.

    see the icon near the java(blue) and resources(yellow 4 lines) folder.

    right-click on java folder and select sources root

    and right-click on java folder and select resource root

    after that create the same package in the java folder and resource folder.

    for example: packages -> org.example

    ├─ src
       ├─ main
           ├─ java
           |  └─ com
           |      └─ example
           |           └─ A.class
           |
           └─ resources  
              └─ com
                  └─ example
                        └─ fxml
                             └─ AFXML.fxml
    

    in A.class you can use this.

    Java

     FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/AFXML.fxml"));
     Parent root = loader.load();
    

    Kotlin

     val loader = FXMLLoader(javaClass.getResource("fxml/AFXML.fxml"))
     val root = loader.load<Parent>()
    

    or

     val loader = FXMLLoader(A::class.java.getResource("fxml/AFXML.fxml"))
     val root = loader.load<Parent>()
    
    0 讨论(0)
  • 2020-12-15 16:47

    If your project is Gradle, check Settings -> Build, Execution, Deployment -> Gradle -> Build and run section. Ensure that "Build and run using" option is "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".

    UPDATED: only 1 option "Build and run using" is enough to set to Gradle.

    0 讨论(0)
  • 2020-12-15 16:51

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题