Troubleshooting系列-找不到资源文件问题分析及解决

问题背景

前期针对一个应用微服务做了启动脚本的参数优化,在本地环境启动运行好好的,但是上了DEV环境后,跑到一个获取资源文件的时候报资源找不到,类似于下面的报错

java 复制代码
java.io.FileNotFoundException: aa.txt
	at com.hyw.util.ResourceUtil.getTextContent(ResourceUtil.java:27)
	at com.toby.sharding.jdbc.source.start.PointShardingApplication.main(PointShardingApplication.java:41)

启动脚本优化前期有文章讲过,具体可以参考这篇落地实践之JAVA应用启动脚本

关键启动脚本参数如下:

setjvment.sh

shell 复制代码
# 设置java.ext.dirs JAVA_OPTS="$JAVA_OPTS -Djava.ext.dirs=$JAVA_HOME/jre/lib/ext:$work_home/lib"

start.sh

shell 复制代码
CLASSPATH=$CLASSPATH:$work_home/lib/conf

问题重现

编写相关问题代码,新增ResourceUtil用来获取资源文件

java 复制代码
public class ResourceUtil {
    private static ResourceUtil resourceUtil = new ResourceUtil();

    public static ResourceUtil getInstance() {
        return resourceUtil;
    }

    public String getTextContent(String fileName) throws FileNotFoundException {
        URL url = ResourceUtil.class.getClassLoader().getResource(fileName);
        System.out.println("default class loader=" + ResourceUtil.class.getClassLoader());
        if (Objects.isNull(url)) {
            System.out.println("default not found");
            url = ClassLoader.getSystemResource(fileName);
            System.out.println("app not found,class loader=" + ResourceUtil.class.getClassLoader());
        }
        if (Objects.isNull(url)) {
            throw new FileNotFoundException(fileName);
        }
        InputStream in = null;
        try {
            in = url.openStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        StringBuilder st = new StringBuilder();
        String line = "";
        try {
            while ((line = bf.readLine()) != null) {
                st.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bf.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(fileName + " context=" + st.toString());
        return st.toString();
    }

    @Test
    public void testCase01() throws FileNotFoundException {
        String content = ResourceUtil.getInstance().getTextContent("a.txt");
        System.out.println(content);
    }


}

新增了两个资源文件

直接在启动类中获取这两个资源文件

java 复制代码
try {
    ResourceUtil.getInstance().getTextContent("a.txt");
    ResourceUtil.getInstance().getTextContent("conf/b.txt");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

打包上传后在windows下运行结果如下:

ini 复制代码
default class loader=sun.misc.Launcher$ExtClassLoader@4b85612c
a.txt context=123
default class loader=sun.misc.Launcher$ExtClassLoader@4b85612c
conf/b.txt context=b.txt

在windows下运行是ok的,但是在unix下确实不行,unix下的截图无法考出来,大概运行结果如下

ini 复制代码
default class loader=sun.misc.Launcher$ExtClassLoader@4b85612c
a.txt context=123
default class loader=sun.misc.Launcher$ExtClassLoader@4b85612c
default not found
app not found,class loader=sun.misc.Launcher$AppClassLoader@4b85612c
conf/b.txt context=b.txt

使用这个class默认的classloader获取不到资源文件,打印出class loader可知是ExtClassLoader

ExtClassLoader是Java中的一个类加载器,它是Java类加载器层次结构中的一部分。其全名为"Extension Class Loader",主要用于加载Java的扩展类库。

因此,按照上述所说,ExtClassLoader不应该加载业务jar,同时可以看出a.txt其实是在另一个jar包中的,也读取不出来,说明ExtClassLoader不适合加载资源文件,具体JDK源码还没细看

修改方案

修改启动脚本,将业务使用的jar都放在app class loader下,即用-cp关联业务jar
setjvment.sh 删除设置java.ext.dirs start.sh修改如下

shell 复制代码
CLASSPATH=$CLASSPATH:$work_home/lib/conf
for file in $work_home/lib/*.jar
do
  if test -f $file
  then
    CLASSPATH=$CLASSPATH:$file
  fi
done

修改完之后运行正常,能够获取资源文件

回顾

为什么本地IDEA运行OK?

抛开系统问题,启动本地利用jps查看启动参数

ruby 复制代码
7968 com.toby.sharding.jdbc.source.start.PointShardingApplication -XX:TieredStopAtLevel=1 -Xverify:none -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:D:\tools\JetBrains\IntelliJ IDEA 2021.3.3\lib\idea_rt.jar=53161:D:\tools\JetBrains\IntelliJ IDEA 2021.3.3\bin -Dfile.encoding=UTF-8

利用arthas追踪jvm参数,应用jar和resource应该都在class-path下

因此不会有问题

加载资源的方式

有两种方式一个是Class类,另一个是ClassLoader类 具体可以参考这篇文章 Java加载资源文件的两种方法

相关推荐
这个昵称不容易18 分钟前
网上摄影工作室系统设计与实现:96 个 Java 文件、115 个接口,作品发布到点赞评论一条线走完
java·springboot·毕设
码事漫谈28 分钟前
AI 提效的真相:省下你的时间,填满你的硬盘
后端
金銀銅鐵1 小时前
带您看懂字节码指令(七):对象创建里有什么——new 与 dup
java·jvm·后端
潍坊老登1 小时前
[非技术文章]过去八个月了,总想写点什么。
面试
AskHarries2 小时前
一个免费工具网站,每个月竟然有近 300 万访问量???
后端
周末程序猿4 小时前
当 99% 的代码由 AI 编写,Code Review 该怎么做?
后端·ai编程
ZhengEnCi5 小时前
前端日志和后端日志怎么记:分级、JSON 格式、按天分文件与保留策略
前端·后端·python
吴名氏.5 小时前
全栈跃迁:Java 后端 27 讲【3】Vue3工程化脚手架
java·开发语言·springboot·全栈工程师
JellyfishMIX6 小时前
AgentScope-Java 核心机制(内循环) ReAct loop 源码分析
java·后端·agent
Rain的Java大神之路7 小时前
Spring Boot 4 把自动配置拆成 47 个模块后,我的 `dependency:tree` 翻天了:一份从排查到治理的完整实战手册
java·spring boot·后端