关键词搜索

源码搜索 ×
×

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

发布2021-09-08浏览3184次

详情内容

主要是因为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);
  • 1
  • 2
  • 3
  • 4

改为

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

但我这里的方式静态的,没有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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

修改后代码

    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;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

参考文章:
*.jar!\BOOT-INF\classes!*.xml没有此文件

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载