JAR文件如何读取外部属性文件

时间:2010-08-11 11:51:40

标签: java properties jar

我们有一个应用程序的连接池组件(JAR文件)。截至目前,应用程序连接详细信息与JAR文件捆绑在一起(在.properties文件中)。

我们可以让它更通用吗?我们可以让客户端告诉属性文件详细信息(路径和文件名)并使用JAR来获取连接吗?

在客户端代码中有这样的东西是否有意义:

XyzConnection con = connectionIF.getConnection(uname, pwd);

除此之外,客户端将指定(以某种方式???)具有要连接的URL,超时等的属性文件详细信息。

7 个答案:

答案 0 :(得分:15)

最简单的方法是,使用-D开关在java命令行上定义系统属性。 该系统属性可能包含属性文件的路径。

E.g

java -cp ... -Dmy.app.properties=/path/to/my.app.properties my.package.App

然后,在您的代码中,您可以这样做(为简洁起见,未显示异常处理):

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
     ...
}

答案 1 :(得分:8)

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");

答案 2 :(得分:6)

只需从文件中加载属性,例如

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编码,也可以省略它,但要注意任何特殊字符。

答案 3 :(得分:1)

最简单的方法如下。它将从jar文件之外的cfg文件夹加载application.properties。

目录结构

  |-cfg<Folder>-->application.properties
  |-somerunnable.jar

代码:

    Properties mainProperties = new Properties();
    mainProperties.load(new FileInputStream("./cfg/application.properties"));
    System.out.println(mainProperties.getProperty("error.message"));

答案 4 :(得分:1)

这是我的解决方案。首先在启动文件夹中查找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"));

答案 5 :(得分:0)

在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);

答案 6 :(得分:-1)

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")

相关问题