Jar或Jboss里无法读取配置文件

以前是通过这种方式加载配置文件:

   private static void init() {
        URL url = PropertyUtil.class.getClassLoader().getResource(
                "ApplicationResources.properties");
        String file = url.getFile();
        try {
            file = java.net.URLDecoder.decode(file, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace() ;
        }

        configFile = new File(file);
        lastModified = configFile.lastModified();
        props = new Properties();
        load();
    }

    private static void load() {
        try {
            props.load(new FileInputStream(configFile));
            lastModified = configFile.lastModified();
        } catch (IOException e) {
            e.printStackTrace() ;
            throw new RuntimeException(e);
        }
    }	
		

可通过以上方式,在Jar包(jar里类读取自己的配置文件)和在jboss-as-7.1.1.Final里(发布的war包项目),总是报找不到配置文件,无法加载,在网上查了一下资料,改成:

        private static void init() {
		props = new Properties();
		load();
	}

	private static void load() {
		try {
			props.load(PropertyUtil.class.getClassLoader().getResourceAsStream("ApplicationResources.properties"));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

就没有问题了,在此记录一下。


你可能感兴趣的:(JavaEE)