ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

发布java包运行提示找不到配置文件

2021-09-08 11:59:54  阅读:217  来源: 互联网

标签:load java 配置文件 提示 jar System println path


主要是因为jar包是一个单独的文件而不是文件夹,不能通过路径的方式来找到配置文件。

一个程序,在开发环境运行得好地地,发布成jar包后,一运行就报错,提示找不到配置文件。提示类似这样的错误:
FileNotFoundException: jar:file:/xxx-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/cert/loadFFmpeg.properties

但事实上,包里文件是存在的。究其原因,主要是因为jar包是一个单独的文件而不是文件夹,不能通过“file:***.jar!\BOOT-INF\classes!\loadFFmpeg.properties”这样的方式来定位jar包内的资源。

网上资源提供了修改示例:

原代码

SAXReader xmlReader = new SAXReader();
String path = URLDecoder.decode(this.getClass().getResource("/").getPath(), "UTF-8");
File xmlFile = new File(path + "/jdbcType.xml");
Document document = xmlReader.read(xmlFile);

改为

SAXReader xmlReader = new SAXReader();
InputStream xmlFile = this.getClass().getResourceAsStream("/jdbcType.xml");
Document document = xmlReader.read(xmlFile);

但我这里的方式静态的,没有this,咋改?其实更简单。

我的代码

原代码

    public static <T> T load(String fileName, Class<T> cl) {
        InputStream is = null;
        String path = "";
        try {
           path = ResourceUtils.getURL("classpath:").getPath();
           path = URLDecoder.decode(path, "utf-8");
           path = String.format("%s%s", path, fileName);
           is = new FileInputStream(path);
        } catch (FileNotFoundException e) {
            System.err.println(String.format("找不到配置文件:%s%s", path, fileName));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if (is != null) {
            Properties pro = new Properties();
            try {
                System.out.println("加载配置文件...");
                pro.load(is);
                System.out.println("加载配置文件完毕");
                return (T) load(pro, cl);
            } catch (IOException e) {
                System.err.println("加载配置文件失败");
                return null;
            }
        }
        
        return null;
    }

修改后代码

    public static <T> T load(String fileName, Class<T> cl) {
    	/*
    	public static final ProgramConfig config = load("loadFFmpeg.properties", ProgramConfig.class);
    	fileName,配置文件名称;cl,读入配置内容的装填对象
    	*/
    	
        InputStream is = cl.getResourceAsStream("/" + fileName);

        if (is != null) {
            Properties pro = new Properties();
            try {
                System.out.println("加载配置文件...");
                pro.load(is);
                System.out.println("加载配置文件完毕");
                return (T) load(pro, cl);
            } catch (IOException e) {
                System.err.println("加载配置文件失败");
                return null;
            }
        }
        
        return null;
	}

标签:load,java,配置文件,提示,jar,System,println,path
来源: https://blog.csdn.net/leftfist/article/details/120176571

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有