从JAR文件加载属性(java 1.6) [英] Loading Properties from a JAR file (java 1.6)

查看:139
本文介绍了从JAR文件加载属性(java 1.6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jar中手动注入一个属性文件。
如何在java 1.7之前从jar文件加载属性?
我尝试了很多解决方法,到目前为止还没有任何工作。

I manually inject a properties file inside a jar. How to load properties from a jar file before java 1.7 ? I tried many workarounds and nothing worked so far.

关于它的问题很多,但一切都集中在java 1.7的ClassLoader方法上。

There's plenty questions about it, but everything is focused on ClassLoader methods from java 1.7.

推荐答案

当你的类路径或jar文件中有一个属性文件时,它就变成了一个资源。任何其他情况都是一个简单的文件。

When you have a properties file inside your classpath or inside your jar file it becomes a resource. Any other case is a simple file.

在打包jar文件之前,你需要做的是将属性文件所在的文件夹添加到类路径中(即myproject / src / main / resources /)然后无论你做什么

What you need to do, before you package your jar file, is add to your classpath the folder where the properties files are (i.e myproject/src/main/resources/) then wherever you do a

Properties properties = new Properties(); 
properties.load(MyClass.class.getResourceAsStream("/yourPropsFileName"));

它会加载它!

虽然,如果您使用外部属性文件,您也可以使用以下方法加载它:

Although, if you are using an external property file you can also load it by using:

Properties properties = new Properties();
properties.load(new FileInputStream("extenalPropsFileLocation"));

希望有所帮助!

这篇关于从JAR文件加载属性(java 1.6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆