Why does class.getResource () continue to return null even though there is a resource at the specified path?

I am wondering why the method getResourcekeeps returning null, I have the following setting:

public static URL getResource(String path){
    URL url = ResourceLoader.class.getResource(path);
    if (Parameters.DEBUG){
        System.out.println(path);
    }
    return url;
}

My project structure in Eclipse is as follows:

-- res
  -- img

The variable pathpassed to getResourceis "/res/img"either or "/res/img/smile.png". However, the method continues to return nulland is urlnot set. I also followed the instructions of this question , which were to add the folder to the project class path through the launch configurations, still without success ... Does anyone know what I'm doing wrong?

enter image description here

+4
source share
2 answers

: "/img/smile.png".

, , , /, Class.getResource, .

, res ​​ classpath. , Class.getResource , . res.

, , res/res/img/smile.png. (), null.

+5

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)

, , . . , ClassLoader.getSystemResource(java.lang.String). :

'/' ('\ u002f'), - , '/'.

: modified_package_name/name modified_package_name '/' '.' ( '\ U002e').

0

All Articles