Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - class.getResource returns null

Tags:

java

url

I am using the following to get the URL of this particular file, but it returns null. Does anyone have any suggestions as to the problem or an alternate way to do this?

URL url = ExchangeInterceptor.class.getResource("GeoIP.dat"); 
like image 512
Buddhi Avatar asked Nov 29 '10 06:11

Buddhi


2 Answers

For those who use Intellij Idea: check for Settings > Build, Execution, Deployment > Compiler > Resource patterns.

The setting contains all extensions that should be interpreted as resources. If an extension does not comply to any pattern here, class.getResource will return null for resources using this extension.

like image 109
yggdraa Avatar answered Sep 19 '22 21:09

yggdraa


The path is relative to the classpath root and if you don't give an absolute path, it is looking in the same package as the class you're using (in this case ExchangeInterceptor). To find something in the root use /GeoIP.dat.

like image 32
Martin Algesten Avatar answered Sep 21 '22 21:09

Martin Algesten