1. Class的getResource() 2. ClassLoader的getResource()

本文详细解析了Java中使用Class.getResource()与ClassLoader.getResource()两种方式读取资源的具体区别,通过实例演示如何获取classpath、特定文件及类路径下的资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在java中的API里,有两种方式来使用classpath读取资源。

1. Class的getResource()

2. ClassLoader的getResource()

但是两者有一定区别,运行以下程序:

复制代码

package zero.xml.config;

public class Main {

    public static void main(String[] args) {
        new Main().testGetResource();
    }
    
    public void testGetResource() {
        
        System.out.println(Main.class.getResource("/").getPath());
        System.out.println(Main.class.getResource("/app.properties").getPath());
        System.out.println(Main.class.getResource("").getPath());
        System.out.println(Main.class.getResource("app.properties").getPath());
        System.out.println("-------------------");
        System.out.println(this.getClass().getResource("/").getPath());
        System.out.println(this.getClass().getResource("/app.properties").getPath());
        System.out.println(this.getClass().getResource("").getPath());
        System.out.println(this.getClass().getResource("app.properties").getPath());
        System.out.println("-------------------");
        System.out.println(Main.class.getClassLoader().getResource("").getPath());
        System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());
        System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());
        System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());
    }
}

复制代码

 

得到输出为:

复制代码

/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config/
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
-------------------
/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config/
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
-------------------
/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties

复制代码

 

也就是:

1. 如果想获得classpath,使用以下方法:

System.out.println(Main.class.getResource("/").getPath());

System.out.println(Main.class.getClassLoader().getResource("").getPath());

 

2. 如果想获得classpath下的文件,使用以下方法:

System.out.println(Main.class.getResource("/app.properties").getPath());

System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());

 

3. 如果想获得当前类(比如zero.xml.config.Main)的路径,使用以下方法:

System.out.println(Main.class.getResource("").getPath());

System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());

 

4. 如果想获得当前类路径下的文件,使用以下方法:

System.out.println(Main.class.getResource("app.properties").getPath());

System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());

 

 

注意,如果获取的文件或路径不存在,getResource()会返回null。比如,getClassLoader().getResource("/")就会返回null。


 

<think>好的,我现在需要回答用户关于如何使用ClassLoader.getResource方法从jar包中加载资源的问题。首先,我得回忆一下相关的知识点。用户提到了引用中的几个方法,比如使用ClassLoadergetResource()方法,还有ResourceUtils.getURL(),但用户现在主要关注的是ClassLoader.getResource的使用。 首先,我应该确认ClassLoader.getResource的基本用法。记得这个方法是通过类加载器来查找资源,资源路径需要是类路径下的相对路径。比如,如果资源在jar包的com/example目录下,那么路径应该是"/com/example/resource.txt",不过可能需要不带开头的斜杠?这里容易混淆,需要再查证一下。 然后,用户提到的示例代码可能需要给出具体的步骤。比如,如何获取ClassLoader实例,如何构建资源路径,如何处理返回的URL对象。引用中的例子显示,可以通过当前类的ClassLoader来获取资源,比如使用MyClass.class.getClassLoader().getResource("burn/burnhex/hex2bin.exe")。这时候需要注意资源路径是否以斜杠开头,或者是否是相对路径。 接下来,需要解释资源路径的写法。比如,当资源位于jar包的根目录时,直接使用文件名;如果在子目录中,需要使用相对于类路径的路径。例如,如果资源在jar包的resources/images目录下,路径应该是"resources/images/image.png"。但要注意,ClassLoader.getResource的参数是否区分绝对和相对路径,绝对路径可能需要以斜杠开头,或者不,这可能取决于具体的类加载器实现。根据Java文档,ClassLoadergetResource方法不接受以斜杠开头的路径,而ClassgetResource方法可以处理,如果路径以斜杠开头,则相对于类路径的根目录,否则相对于当前类所在的包。所以这里使用ClassLoader.getResource时,路径应该是不带斜杠的绝对路径,或者相对路径? 例如,引用[2]中的例子是加载"burn/burnhex/hex2bin.exe",没有以斜杠开头,说明使用的是相对于类路径根目录的路径。所以,正确的做法是,资源路径应该相对于类路径的根,即从jar包的根目录开始,但不带前导斜杠。例如,如果资源在jar包的com/example/res目录下,路径应该是"com/example/res/file.txt"。 然后,需要给出一个具体的Java代码示例。比如,获取ClassLoader实例,调用getResource方法,并处理URL。比如: ClassLoader classLoader = MyClass.class.getClassLoader(); URL resourceUrl = classLoader.getResource("com/example/resource.txt"); if (resourceUrl != null) { // 读取资源 } 此外,需要提到如何处理资源可能不存在的情况,比如检查返回的URL是否为null。同时,可能需要解释如何从URL中获取输入流,使用getResourceAsStream方法,但用户的问题是关于getResource的,所以重点在获取URL。 另外,引用[4]中提到,当使用JarLoader时,会按jar包的方式读取资源。所以,当资源被打包进jar后,ClassLoader能够正确找到它,只要路径正确。 总结步骤应该是: 1. 确定资源在jar包中的路径,相对于类路径根目录。 2. 使用ClassLoadergetResource方法传入该路径。 3. 处理返回的URL,可能转换为文件路径或读取内容。 同时需要注意,当资源在jar包中时,通过URL获取路径可能会是jar:file:/path/to.jar!/resource/path这样的形式,不能直接作为文件路径访问,需要使用特定的方法读取,比如通过URL.openStream()。 可能还需要提醒用户,如果资源在jar包中,直接转换为文件路径可能会失败,必须通过流来读取内容。 最后,生成相关问题,比如如何处理资源不存在,如何读取资源内容,路径格式问题等。</think>要使用 `ClassLoader.getResource()` 方法从 JAR 包中加载资源文件,需遵循以下步骤: --- ### **1. 资源路径规范** 资源路径需相对于**类路径根目录**,且**不以前导斜杠开头**。例如: - 若资源位于 JAR 包的 `com/example/resources` 目录下,完整路径应为 `com/example/resources/file.txt`[^3][^4]。 - 若资源在 JAR 包根目录下,直接使用文件名,如 `config.properties`。 --- ### **2. 代码示例** ```java public class ResourceLoader { public static void main(String[] args) { // 获取当前类的 ClassLoader ClassLoader classLoader = ResourceLoader.class.getClassLoader(); // 加载资源文件(路径相对于类路径根目录) URL resourceUrl = classLoader.getResource("com/example/config.xml"); if (resourceUrl != null) { System.out.println("资源路径: " + resourceUrl.getPath()); // 示例输出: jar:file:/path/to/your.jar!/com/example/config.xml } else { System.out.println("资源未找到"); } } } ``` --- ### **3. 关键注意事项** - **路径格式**:使用 `/` 作为目录分隔符,且路径需与 JAR 包内结构完全一致[^3]。 - **资源访问方式**:若资源在 JAR 包内,`resourceUrl.getPath()` 返回的路径可能包含 `jar:file:` 前缀,需通过 `InputStream` 读取内容: ```java InputStream inputStream = classLoader.getResourceAsStream("com/example/config.xml"); ``` - **空值检查**:务必检查返回的 `URL` 是否为 `null`,避免资源不存在时抛出异常[^2]。 --- ### **4. 常见问题解决** - **路径错误**:若返回 `null`,检查路径是否与 JAR 包内结构一致,或使用 `jar -tf your.jar` 命令验证。 - **读取资源内容**:通过 `ClassLoader.getResourceAsStream()` 直接获取输入流,无需解析文件系统路径[^1]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值