我编写了一个加载外部属性的类,以便在我的程序中使用,它是通过以下链接建模的:Load Properties File in Singleton Class。我在这里阅读(FileNotFoundException with properties file)和这里(Java Properties File not loading)如何加载外部属性文件,但是我继续得到FileNotFoundException。我的属性文件是外部文件,与我的可执行jar文件位于同一目录中。
公共类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 -jar chef-deploy-tests-0.0.0009-SNAPSHOT-jar-with-dependencies.jar test.properties
有关我做错的任何建议吗?
答案 0 :(得分: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 '.'}.
答案 1 :(得分:0)
我认为您的问题是test.properties不在您的类路径上。现在运行它的方法就是将文件作为参数传递给应用程序。我认为您的意思是将其添加到应用程序的类路径中:
java -jar chef-deploy-tests-0.0.0009-SNAPSHOT-jar-with-dependencies.jar -classpath。
这将包括java类路径中当前目录中的所有文件。另一种选择可能是将test.properties文件放入一个单独的目录中并指定一个而不是&#39;。&#39;