java web项目 , jar包如何读取配置文件

本文探讨了Java项目中如何在web项目和jar包环境下正确读取`application.properties`配置文件,重点讲解了三种方法的使用场景和差异,并揭示了在jar包运行时遇到的常见问题及其解决方案。
摘要由CSDN通过智能技术生成
java 项目结构:

java项目结构
application.properties

app.version=1.0
getClassLoader().getResource(“application.properties”)只支持web项目读取配置文件。
getClassLoader().getResourceAsStream(“application.properties”)支持web项目和jar包读取配置文件。
完整代码:
package com.test;

import java.io.*;
import java.net.URL;
import java.util.Properties;

public class JavaPath {

    /**
     * 读取配置文件,支持在web项目中和以jar包的形式读取。
     */
    public static void bothJarAndWeb1() {
        InputStream inputStream = JavaPath.class.getClassLoader().getResourceAsStream("application.properties");
        commonMethod(inputStream);
    }

    /**
     * 读取配置文件,支持在web项目中和以jar包的形式读取。
     */
    public static void bothJarAndWeb2(){
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
        commonMethod(inputStream);
    }

    /**
     * 读取配置文件,只支持在web项目中读取。
     */
    public static void onlyWeb(){
        URL resource = JavaPath.class.getClassLoader().getResource("application.properties");
        File file = new File(resource.getPath());
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        commonMethod(inputStream);
    }

    public static void commonMethod(InputStream inputStream){
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("app.version=" + properties.get("app.version"));
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args)  {
        System.out.print("bothJarAndWeb1 output: ");
        JavaPath.bothJarAndWeb1();
        System.out.print("bothJarAndWeb2 output: ");
        JavaPath.bothJarAndWeb2();
        System.out.print("onlyWeb output: ");
        JavaPath.onlyWeb();
    }
}

直接在idea中运行输出结果:

bothJarAndWeb1 output: app.version=1.0
bothJarAndWeb2 output: app.version=1.0
onlyWeb output: app.version=1.0

打包成jar后运行输出结果:

java -jar javapath.jar                     
bothJarAndWeb1 output: app.version=1.0
bothJarAndWeb2 output: app.version=1.0
onlyWeb output: java.io.FileNotFoundException: file:/Users/xiaosan/Documents/work/javapath/out/artifacts/javapath_jar/javapath.jar!/application.properties (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:120)
        at com.test.JavaPath.onlyWeb(JavaPath.java from InputFileObject:48)
        at com.test.JavaPath.main(JavaPath.java from InputFileObject:61)
Exception in thread "main" java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Properties.java:418)
        at java.util.Properties.load0(Properties.java:337)
        at java.util.Properties.load(Properties.java:325)
        at com.test.JavaPath.commonMethod(JavaPath.java from InputFileObject:14)
        at com.test.JavaPath.onlyWeb(JavaPath.java from InputFileObject:52)
        at com.test.JavaPath.main(JavaPath.java from InputFileObject:61)

注意错误file:/Users/xiaosan/Documents/work/javapath/out/artifacts/javapath_jar/javapath.jar!/application.properties (No such file or directory)中的javapath.jar!/application.properties把jar文件当做了目录。

web项目与jar包的区别
  • web项目放在servlet容器中运行,解压后是多个目录和文件,可以通过资源路径找到文件。
  • jar包只是一个jar文件,不能通过资源路径寻找配置文件。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值