使用Maven ClassLoader.getSystemClassLoader()。getResource()返回null(Using Maven ClassLoader.getSystemClassLoader().getResource() return null)

我的项目目录结构(在Eclipse中):

MyProject/ src/main/Java src/main/resources stories file.story

在Main类中,我通过MAVEN在返回null的同时返回以下行返回null

String folderName = "stories"; URL appURL = ClassLoader.getSystemClassLoader().getResource(folderName);

在通过maven执行时,appURL返回NULL。

通过阅读Stackoverflow上的一篇文章,我得知道,我在服务器上运行webapp但是没有引用服务器上的资源,所以我们需要在POM.xml文件中添加一些代码。 我在POM.xml文件中添加了以下代码,但仍然无法正常工作:(

<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>stories</include> </includes> </resource> </resources>

寻求帮助。

My project directory structure (in Eclipse):

MyProject/ src/main/Java src/main/resources stories file.story

In Main class i have return below line which return null while excuting main class through MAVEN

String folderName = "stories"; URL appURL = ClassLoader.getSystemClassLoader().getResource(folderName);

While executing through maven, appURL returns NULL.

By reading one post on Stackoverflow i got to kanow that, i am running the webapp on server but there is no reference to the resources on the server so we need to add some code in POM.xml file. i have added below code in POM.xml file but still its not working :(

<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>stories</include> </includes> </resource> </resources>

Looking for help.

最满意答案

有两种方法可以实现这一目标

方法1)

如果它不在主方法URL url = getClass()。getClassLoader()。getResource(“someresource.xxx”); 如果它在main中,则需要创建类的对象,然后在其上调用getClass。

方法2)

通过扩展URLStreamHandler

import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; /** A {@link URLStreamHandler} that handles resources on the classpath. */ public class YourClass extends URLStreamHandler { /** The classloader to find resources from. */ private final ClassLoader classLoader; public YourClass() { this.classLoader = getClass().getClassLoader(); } public YourClass(ClassLoader classLoader) { this.classLoader = classLoader; } @Override protected URLConnection openConnection(URL u) throws IOException { final URL resourceUrl = classLoader.getResource(u.getPath()); return resourceUrl.openConnection(); } }

参考: 用于从Java中的类路径加载资源的URL

There are two ways you can achieve this

Approach 1)

If it is not inside the main method URL url = getClass().getClassLoader().getResource("someresource.xxx"); If it is inside main you need to create object of your class and then call getClass on it .

Approach 2)

By extending URLStreamHandler

import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; /** A {@link URLStreamHandler} that handles resources on the classpath. */ public class YourClass extends URLStreamHandler { /** The classloader to find resources from. */ private final ClassLoader classLoader; public YourClass() { this.classLoader = getClass().getClassLoader(); } public YourClass(ClassLoader classLoader) { this.classLoader = classLoader; } @Override protected URLConnection openConnection(URL u) throws IOException { final URL resourceUrl = classLoader.getResource(u.getPath()); return resourceUrl.openConnection(); } }

Ref:URL to load resources from the classpath in Java

更多推荐