访问.jar外的属性文件? [英] Accessing properties files outside the .jar?
问题描述
我有一个.jar文件,我正在整理。我想创建一个非常简单的.properties文件,其中包含可配置的内容,例如用户名和文件。其他的东西,所以他们可以手工编辑,而不是我必须包括一个GUI编辑器。
I have a .jar file I'm putting together. I want to create a really really simple .properties file with configurable things like the user's name & other stuff, so that they can hand-edit rather than my having to include a GUI editor.
我想做的是能够搜索,此订单:
What I'd like to do is to be able to search, in this order:
- 指定的属性文件(
args [0]
) - 当前目录中的MyApp.properties(调用Java的目录)
- 用户目录中的MyApp.properties( user.home 系统属性?)
- MyApp存储应用程序.jar的目录中的.properties
- a specified properties file (
args[0]
) - MyApp.properties in the current directory (the directory from which Java was called)
- MyApp.properties in the user's directory (the user.home system property?)
- MyApp.properties in the directory where the application .jar is stored
我知道如何访问#1和#3(我认为),但是如何在运行时#2和#4确定?
I know how to access #1 and #3 (I think), but how can I determine at runtime #2 and #4?
推荐答案
#2是user.dir系统属性。 #3是user.home属性。
#2 is the "user.dir" system property. #3 is the "user.home" property.
#4无论你怎么接近它都有点像kludge。如果您从JAR加载的类不在系统类路径上,则这是一种替代技术。
#4 is a bit of a kludge no matter how you approach it. Here's an alternate technique that works if you have a class loaded from a JAR not on the system classpath.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL url = new URL(src.getLocation(), "MyApp.properties");
...
}
else {
/* Fail... */
}
这篇关于访问.jar外的属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!