Java Gradle项目中的资源正确获取方式

作者:Allocator 时间:2022-10-05 09:00:50 

引言

一个Java Gradle项目会涉及到资源的访问. 一般情况下会将当前项目所需的资源文件全部放置于resources文件夹下, 无论是main文件下的source code 还是test文件夹下的test code.

都或多或少的涉及到获取resources文件夹下的资源. 本文主要目的就是详细的总结一下如何获取resources文件夹下的资源.

两个getResource方法

来看一个简单的Java Gradle项目(称呼其为simpleresource)的项目结构

Java Gradle项目中的资源正确获取方式

首先这个project执行build之后会在根目录下创建一个out目录, 这个目录存放所有的编译结果(class文件以及资源文件). 如上图所示production文件夹对应的是source code而test文件夹对应的是test code.

所有的资源都会存储在resources文件夹内部. 当程序运行时获取的资源就是这个resources文件夹下的资源.

我们使用最多的获取资源的方法有两个 Class.getResource 和 ClassLoader.getResource 但是这两个方法传递参数与结果不同, 下面详细分析一下这两个方法参数以及返回值.

先看 ClassLoader 中的 getResource 方法. 只需要获取类加载器对象即可(获取方式不再赘述). 先看这个方法的API文档相关的描述:

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code. The name of a resource is a '/'-separated path name that identifies the resource.

This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.

从这个描述中可以得知提供资源的路径(我理解的是相对路径), 正常情况下该方法会返回资源完整的URL. 传递的参数有一个重要的注意事项, 就是传递的参数不能够以/ 开始, 这也是我为什么称呼这个参数为资源的相路径. 举个例子

URL test = this.getClass().getClassLoader().getResource("/");

运行上述代码返回的结果是:

Java Gradle项目中的资源正确获取方式

参考simpleresource的项目结构, 正确获取 com.mainres 下的文件的正确做法是:

String name = "com/mainres/testmain.txt";

URL test = this.getClass().getClassLoader().getResource(name);

结果为:

Java Gradle项目中的资源正确获取方式

如果在表示资源路径的字符串中加上 / 那么获取到的URL依然为null

String name = "/com/mainres/testmain.txt";

URL test = this.getClass().getClassLoader().getResource(name);

Java Gradle项目中的资源正确获取方式

宗上所述, 使用类加载器获取资源的方式传递的参数为资源相对路径(相对于resources文件夹的路径), 既然是相对路径自然参数 不能够以 / 开始.

有一个问题需要注意, 当传递参数为空字符串的时候, 得到路径其实是classes文件夹的绝对路径, 但当传递一个正确的资源路径相对字符串时, 得到路径却是resources文件夹下的资源路径.

String name = "";

URL test = this.getClass().getClassLoader().getResource(name);

Java Gradle项目中的资源正确获取方式

我的理解是本质上是通过此方法获取的其实类加载器加载的class字节码目录, 所以使用空字符串会看到实际输出的是classes文件夹绝对路径, 当传递正确的资源路径的时候, 代码层面做转换, 转而获取与classes文件夹同级的resources文件夹下的资源.

再看 Class 中的 getResurce 方法

由于这个方法传递参数是否是以 / 开头会产生不同的结果, 且使用这个方法也比较容易和 ClassLoader 中的 getResource 方法搞混淆, 所以本文多次强调传递的参数是否以 / 开始.

首先看传递参数为 "" 和 / 的两种情况得到的结果:

使用空字符串:

String name = "";

URL test = this.getClass().getResource(name);

运行结果:

Java Gradle项目中的资源正确获取方式

使用 /

String name = "/";

URL test = this.getClass().getResource(name);

运行结果为:

Java Gradle项目中的资源正确获取方式

最大的区别是使用空字符串 "" 获取的路径是相对于包的目录, 而使用 / 获取的路径是类加载器加载class文件的目录, 这个和 ClassLoader 的 getResource 方法传递 "" 字符串的结果是一样的. 所以如果要正确的获取到资源文件,

那么使用 Class 的 getResource 方法如下:

String name = "/com/mainres/testmain.txt";

URL test = this.getClass().getResource(name);

运行结果:

Java Gradle项目中的资源正确获取方式

所以综上所述, 一个简单的防止两个方法传递参数搞混淆的记忆方式就是使用 Class 的 getResource 方法需要加 / 而使用 ClassLoader 的 getResource 方法不要加 /.

其实参考 Class 类中的 getResource 方法:


public java.net.URL getResource(String name) {
 name = resolveName(name);
 ClassLoader cl = getClassLoader0();
 if (cl==null) {
  // A system class.
  return ClassLoader.getSystemResource(name);
 }
 return cl.getResource(name);
}

本质上讲它也是调用ClassLoader 中的getResource 方法. 其中resolveName 这个方法对传递的参数做了转换.


private String resolveName(String name) {
 if (name == null) {
  return name;
 }
 if (!name.startsWith("/")) {
  Class<?> c = this;
  while (c.isArray()) {
   c = c.getComponentType();
  }
  String baseName = c.getName();
  int index = baseName.lastIndexOf('.');
  if (index != -1) {
   name = baseName.substring(0, index).replace('.', '/')
    +"/"+name;
  }
 } else {
  name = name.substring(1);
 }
 return name;
}

当传递的参数带有/ 时候, resolveName 会将/ 去除后的字符串返回, 最后调用ClassLoader 中的 getResource 方法.

小结

本文对比了一下Class 和 ClassLoader 中的getResource 方法的差异,如果单纯从资源的获取角度来看最终调用的都是ClassLoader 中的getResource 方法.

简单记忆即是使用Class 的getResource 方法资源路径需要加/ 而使用ClassLoader 中的getResource 方法则不需要加/.

来源:https://blog.csdn.net/Allocator/article/details/80814326

标签:Java,Gradle,资源
0
投稿

猜你喜欢

  • Android AutoCompleteTextView连接数据库自动提示的方法(附demo源码下载)

    2023-09-23 14:53:20
  • java多线程入门知识及示例程序

    2021-11-30 03:17:58
  • java实现Socket通信之单线程服务

    2022-08-24 14:41:06
  • springboot2.5.6集成RabbitMq实现Topic主题模式(推荐)

    2021-10-03 22:01:22
  • Flutter 设置全局字体的实现

    2023-05-04 06:27:25
  • spring boot启动加载数据原理分析

    2021-06-16 07:56:15
  • java通过Jsoup爬取网页过程详解

    2021-12-20 03:24:10
  • Android仿淘宝物流信息TimeLineView

    2023-10-09 20:12:47
  • Java实现计算器设计

    2023-08-18 13:36:54
  • Eclipse 开发java 出现Failed to create the Java Virtual Machine错误解决办法

    2021-12-24 10:35:50
  • MyBatis学习教程(三)-MyBatis配置优化

    2023-10-31 21:13:51
  • Java NIO实例UDP发送接收数据代码分享

    2022-12-03 01:30:37
  • Android APK应用安装原理解析之AndroidManifest使用PackageParser.parserPackage原理分析

    2023-05-19 23:58:18
  • c#文件名/路径处理方法示例

    2021-11-28 21:02:40
  • Android仿google now效果的呼吸按钮

    2023-06-17 07:58:02
  • Java+Ajax实现的用户名重复检验功能实例详解

    2022-12-01 12:54:46
  • Flutter SizedBox布局组件Widget使用示例详解

    2022-02-08 18:19:44
  • string类的使用方法详解

    2022-04-05 12:01:51
  • 深入分析C# 线程同步

    2023-05-09 00:49:44
  • java实现MD5加密算法的实例代码

    2021-10-01 16:49:14
  • asp之家 软件编程 m.aspxhome.com