Debugging getResource*

前端 未结 5 773
心在旅途
心在旅途 2021-01-07 22:16

How do you debug getResource-style methods that are failing, returning null?

I am sure the file it\'s looking for is there, but it\'s returning NULL. How do I know w

5条回答
  •  别那么骄傲
    2021-01-07 22:32

    Since getResource() searches the classpath (as others have mentioned), it might be helpful to dump the actual classpath being searched before your problemsome getResource() call:

    log.debug("classpath is: " + System.getProperty("java.class.path"));
    
    //the line that is returning null
    ... = Thread.currentThread().getContextClassLoader().getResource("foobar");
    

    What is probably happening is that the files/directories you think are on the classpath are actually not (perhaps an invalid path is being set somewhere along the way).

提交回复
热议问题