使用getClassLoader()加载mmdb文件.getResource()返回null,尽管该文件存在于资源中



我有一段代码:

class GeoLocator{
public GeoLocator() throws IOException {
final URL url = getClass().getClassLoader().getResource("GeoLite2-Country.mmdb");
if (url == null) {
throw new IOException("File not found!");
}
}
}

在我的主要课程中:

public class Main {
public static void main(String[] args) {
try {
GeoLocator geoLocator = new GeoLocator();
} catch (IOException e) {
e.printStackTrace();
}
}
}

我的目标是加载一个位于项目资源中的mmdb文件。当我运行主函数时,我发现一个异常:

java.io.IOException: File not found!
at com.a.GeoLocator.<init>(GeoLocator.java:19)
at com.a.Main.main(Main.java:11)

我的项目中的文件层次结构如下:

-main
-java
-com.a
GeoLocator.java
Main.java
-resources
GeoLite2-Country.mmdb

我曾尝试使用绝对路径和相对路径运行代码,但没有成功。

我正在使用IntelliJ

这有什么关系吗?

缺少的是:

<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/GeoLite2-Country.mmdb</include>
<include>**/apache.log</include>
</includes>
</resource>
</resources>

在pom.xml文件中

最新更新