JAR文件如何读取外部属性文件
我们有一个应用程序的连接池组件(JAR文件)。 截至目前,应用程序连接详细信息与JAR文件(在.properties
文件中)捆绑在一起。
我们可以让它更通用吗? 我们可以让客户端告诉属性文件详细信息(路径和文件名)并使用JAR来获取连接吗?
在客户端代码中有这样的东西是否有意义:
XyzConnection con = connectionIF.getConnection(uname, pwd);
除此之外,客户端将指定(以某种方式???)具有要连接的URL,超时等的属性文件详细信息。
http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
有多种方法,上面的文章提供了更多细节
ClassLoader.getResourceAsStream ("some/pkg/resource.properties"); Class.getResourceAsStream ("/some/pkg/resource.properties"); ResourceBundle.getBundle ("some.pkg.resource");
最简单的方法是,使用-D开关在java命令行上定义系统属性。 该系统属性可能包含属性文件的路径。
例如
java -cp ... -Dmy.app.properties=/path/to/my.app.properties my.package.App
然后,在您的代码中,您可以执行(为简洁起见,未显示exception处理):
String propPath = System.getProperty( "my.app.properties" ); final Properties myProps; if ( propPath != null ) { final FileInputStream in = new FileInputStream( propPath ); try { myProps = Properties.load( in ); } finally { in.close( ); } } else { // Do defaults initialization here or throw an exception telling // that environment is not set ... }
只需加载文件中的属性即可
Properties properties = new Properties(); InputStreamReader in = null; try { in = new InputStreamReader(new FileInputStream("propertiesfilepathandname"), "UTF-8"); properties.load(in); } finally { if (null != in) { try { in.close(); } catch (IOException ex) {} } }
请注意如何将编码明确指定为上面的UTF-8。 如果您接受默认的ISO8859-1编码,也可以省略它,但要注意任何特殊字符。
最简单的方法如下。 它将从jar文件外部的cfg文件夹加载application.properties。
目录结构
|-cfg-->application.properties |-somerunnable.jar
码:
Properties mainProperties = new Properties(); mainProperties.load(new FileInputStream("./cfg/application.properties")); System.out.println(mainProperties.getProperty("error.message"));
这是我的解决方案。 首先在启动文件夹中查找app.properties
,如果不存在,请尝试从JAR包加载:
File external = new File("app.properties"); if (external.exists()) properties.load(new FileInputStream(external)); else properties.load(Main.class.getClassLoader().getResourceAsStream("app.properties"));
在netbeans中,我需要从jar文件之外的conf/
文件夹中加载application.properties。
因此我写道:
public static String getProperty(String FileName, String Prop) { try { FIS = new FileInputStream( "./../conf/"+FileName); Properties properties; (properties = new Properties()).load(FIS); for(Enumeration propKeys = properties.propertyNames(); propKeys.hasMoreElements();){ String tmpKey = (String) propKeys.nextElement(); String tmpValue = properties.getProperty(tmpKey); tmpValue = tmpValue.trim(); if (tmpKey.equals(Prop)){ //System.out.println(tmpKey +"="+tmpValue); properties.put(tmpKey, tmpValue); Value = properties.getProperty(Prop); break; } } if (Value==null){ throw new Exception("La Propiedad : "+Prop+" no Se encuentra en el Archivo de Configuracion"); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return Value; }
对于Eclipse,请应用以下内容:
FIS = new FileInputStream( "../conf/"+FileName);
public static String getPropertiesValue(String propValue) { Properties props = new Properties(); fileType = PCLLoaderLQIOrder.class.getClassLoader().getResourceAsStream(propFileName); if (fileType != null) { try { props.load(fileType); } catch (IOException e) { logger.error(e); } } else { try { throw new FileNotFoundException("Property file" + propFileName + " not found in the class path"); } catch (FileNotFoundException e) { logger.error(e); } } String propertiesValue = props.getProperty(propValue); return propertiesValue; }
上面的方法适用于我,只需将属性文件存储到运行jar的目录中,并提供该名称来代替propFileName,当你想从属性中获取任何值时,只需调用getPropertyValue("name")
。