java 从 JAR 目录读取属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1301373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading properties file from JAR directory
提问by Lehane
I'm creating an executable JAR that will read in a set of properties at runtime from a file. The directory structure will be something like:
我正在创建一个可执行 JAR,它将在运行时从文件中读取一组属性。目录结构将类似于:
/some/dirs/executable.jar
/some/dirs/executable.properties
Is there a way of setting the property loader class in the executable.jar file to load the properties from the directory that the jar is in, rather than hard-coding the directory.
有没有办法在 executable.jar 文件中设置属性加载器类,以从 jar 所在的目录加载属性,而不是对目录进行硬编码。
I don't want to put the properties in the jar itself as the properties file needs to be configurable.
我不想将属性放在 jar 本身中,因为属性文件需要可配置。
回答by Adamski
Why not just pass the properties file as an argument to your main method? That way you can load the properties as follows:
为什么不将属性文件作为参数传递给 main 方法?这样,您可以按如下方式加载属性:
public static void main(String[] args) throws IOException {
Properties props = new Properties();
props.load(new BufferedReader(new FileReader(args[0])));
System.setProperties(props);
}
The alternative: If you want to get the current directory of your jar file you need to do something nasty like:
替代方案:如果您想获取 jar 文件的当前目录,则需要执行以下操作:
CodeSource codeSource = MyClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
File jarDir = jarFile.getParentFile();
if (jarDir != null && jarDir.isDirectory()) {
File propFile = new File(jarDir, "myFile.properties");
}
... where MyClassis a class from within your jar file. It's not something I'd recommend though - What if your app has multiple MyClassinstances on the classpath in different jar files (each jar in a different directory)? i.e. You can never really guarantee that MyClasswas loaded from the jar you think it was.
... MyClassjar 文件中的类在哪里。这不是我推荐的东西 - 如果您的应用程序MyClass在不同 jar 文件(每个 jar 位于不同目录)的类路径上有多个实例怎么办?即你永远不能真正保证它MyClass是从你认为的罐子里加载的。
回答by amsathishkumar
public static void loadJarCongFile(Class Utilclass )
{
try{
String path= Utilclass.getResource("").getPath();
path=path.substring(6,path.length()-1);
path=path.split("!")[0];
System.out.println(path);
JarFile jarFile = new JarFile(path);
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
if (entry.getName().contains(".properties")) {
System.out.println("Jar File Property File: " + entry.getName());
JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
InputStream input = jarFile.getInputStream(fileEntry);
setSystemvariable(input);
InputStreamReader isr = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Jar file"+line);
}
reader.close();
}
}
}
catch (Exception e)
{
System.out.println("Jar file reading Error");
}
}
public static void setSystemvariable(InputStream input)
{
Properties tmp1 = new Properties();
try {
tmp1.load(input);
for (Object element : tmp1.keySet()) {
System.setProperty(element.toString().trim(),
tmp1.getProperty(element.toString().trim()).trim());
}
} catch (IOException e) {
System.out.println("setSystemvariable method failure");
}
}

