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

11
我们有一个连接池组件(JAR文件)用于我们的应用程序。目前,应用程序连接详细信息与JAR文件捆绑在一起(在.properties文件中)。
我们能否使其更加通用?客户端是否可以告诉属性文件详细信息(包括路径和文件名),并使用JAR获取连接?
是否有意义在客户端代码中添加类似以下内容:
XyzConnection con = connectionIF.getConnection(uname, pwd);

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

7个回答

21

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

例如:

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

9

9

只需从文件中加载属性,类似于以下内容:

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编码,则可以将其省略,但请注意任何特殊字符。

5

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

这很简单而且不错,对我的宠物项目有帮助。 - garykwwong

3

最简单的方法如下。它将从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"));

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

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


1
那是不清晰的。 - Dave Newton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接