2014-10-06 95 views
0

我已经写了一个类,加载我的程序中使用的外部属性,它建模了这个链接:Load Properties File in Singleton Class。我已阅读(FileNotFoundException with properties file)和这里(Java Properties File not loading)如何加载外部属性文件,但我继续得到FileNotFoundException。我的属性文件是外部的,位于与我的可执行文件相同的目录中。Java - 加载外部属性文件 - FileNotFoundException

公共类ExternalProperties扩展属性{

private static ExternalProperties instance = null; 
private Properties properties; 
private static String location; 

protected ExternalProperties() throws IOException { 
    properties = new Properties(); 
    properties.load(getClass().getResourceAsStream("test.properties")); 
} 

public static ExternalProperties getInstance(){ 
    if(instance == null){ 
     try{ 
      instance = new ExternalProperties(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return instance; 
} 

public static void setLocation(String path){ 
    location = path; 
} 

}

我传递的属性文件的位置,通过命令行如:

Java的罐子厨师 - 部署 - 测试-0.0.0009-SNAPSHOT-jar -with-dependencies.jar test.properties

对我在做什么有什么建议吗?

回答

0

这取决于你的test.properties文件的位置。

* <li> If the name begins with a {@code '/'} 
* then the absolute name of the resource is the 
* portion of the name following the {@code '/'}. 
* 
* <li> Otherwise, the absolute name is of the following form: 
* 
* <blockquote> 
* {@code modified_package_name/name} 
* </blockquote> 
* 
* <p> Where the {@code modified_package_name} is the package name of this 
* object with {@code '/'} substituted for {@code '.'}. 
0

我认为你的问题是test.properties不在你的类路径中。现在运行它的方式只是将文件作为参数传递给应用程序。我认为你所要做的就是将它添加到应用程序的类路径中:

java -jar chef-deploy-tests-0.0.0009 -snapshot-jar -with-dependencies.jar -classpath。

这将包括java classpath中当前目录中的所有文件。另一个选择可能是将该test.properties文件放入一个单独的目录并指定一个而不是'。'。