简体   繁体   English

如何在Runnable JAR中存储和检索属性?

[英]How do I store and retrieve Properties inside Runnable JAR?

I'm trying to use Java class Properties for saving the settings for my application between executions. 我正在尝试使用Java类属性在执行之间保存应用程序的设置。 Afterwards I would like to export my application into runnable JAR file and keep everything needed in this one file to provide portability. 之后我想将我的应用程序导出到可运行的JAR文件中,并保留这个文件中所需的所有内容以提供可移植性。 So my application has to find the properties file, load it and then save new settings into it on exit. 所以我的应用程序必须找到属性文件,加载它然后在退出时将新设置保存到其中。

The loading works fine: 装载工作正常:

Properties prop = new Properties().load(this.getClass().getResourceAsStream("properties.cfg"));

But for storing I need to find the same file and overwrite it. 但是对于存储,我需要找到相同的文件并覆盖它。 The method Properties.store() needs an OutputStream but getResourceAsStream returns an InputStream therefore I cannot find the file and access it. 方法Properties.store()需要一个OutputStream,但getResourceAsStream返回一个InputStream,因此我找不到该文件并访问它。

It should be always located with the *.class file. 它应始终位于* .class文件中。 I found many solutions that work before exporting into JAR but none that would work after exporting. 我发现许多解决方案在导出到JAR之前有效但没有一个在导出后可以工作。 Thanks for any suggestions. 谢谢你的任何建议。

You can't rewrite your jar (or rather, it's complicated and not a good idea). 你不能重写你的jar(或者更确切地说,它很复杂并且不是一个好主意)。

A preferable solution would be to read the properties from: 一个更好的解决方案是从以下位置读取属性:

  1. a directory on your machine 您计算机上的目录
  2. the .jar file .jar文件

Initially location 1 wouldn't have a property file and you'd fall back to your .jar file. 最初,位置1没有属性文件,您将回退到.jar文件。 When you write the properties you'd write them to the nominated directory, and then on the next read you'd read your properties from this location. 当您编写属性时,您将它们写入指定目录,然后在下一次阅读时,您将从此位置读取属性。 This would survive restarts etc. 重启等等。

Note that libraries such as Apache Commons Configuration automatically support this tiered properties location mechanism and may save you some time/grief in writing your own solution. 请注意,诸如Apache Commons Configuration之类的库会自动支持这种分层属性定位机制,并且可以为您编写自己的解决方案节省一些时间/精力。

For this kind of scenario the best approach is to add the properties file to the classpath and load it using something like ResourceBundle. 对于这种情况,最好的方法是将属性文件添加到类路径并使用ResourceBundle之类的东西加载它。

private static ResourceBundle _properties = ResourceBundle.getBundle("sample.properties");
String sampleProperty = _properties.getString(propertyKey);

How to execute : 如何执行:

Suppose your jar file is MyJar.jar and property file is called sample.properties then u should write a batch or sh file around it 假设您的jar文件是MyJar.jar,属性文件名为sample.properties,那么你应该在它周围写一个批处理文件或sh文件

eg: run.sh 例如:run.sh

java -cp MyJar.jar:conf "Class with main Method"

take spring's IO infrastructure (ClasspathResource) 采用spring的IO基础结构(ClasspathResource)

try {
        ClassPathResource r = new ClassPathResource("bdd.properties");
        Properties properties = new Properties();
        properties.load(r.getInputStream());
    } catch (IOException e) {
        Throwables.propagate(e);
    }

this is real simplification. 这是真正的简化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM