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");
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");
I've faced with the similar problem. From Java SE API for getResource(String name) :
So I've added
'/'
before my directory :MyClass.class.getResource("/dir_name/")
.In your case try to add
'/'
before your file name:URL url = ExchangeInterceptor.class.getResource("/GeoIP.dat");
Use the
getResource
method of the class'ClassLoader
No, that is the right way afaik. Make sure the resource is on your classpath. This is often the cause of these types of problems.
I solved this problem by pointing out the
resource root
on IDEA.Initially the directory was so and the icon was a plain folder icon
Before
Right click
on a directory (or just the project name) ->Mark directory As
->Resource Root
.After
Recompile & rejoice :P
The file needs to be in the classpath, e.g.: -
bin/my/package/GeoIP.dat
The / prefix seems to be a lie. The following would work.
I suspect the issue is that you do not have the file in the classpath.
While using IntelliJ, I generated the project as a JavaFX app and then added maven framework support to it. Turns out, I then placed my resource in
src/main/resources
and had to add./
behind every resource name that while using them in the code.Also as stated in a previous answer, only loading the resource by a classLoader worked.
So for me, the final URL loading was done using:
URL url = getClass().getClassLoader().getResource(String.format(".%ssample.fxml", File.separatorChar));
The
File.separatorChar
returns/
on *nix and\
on windows.