从类路径中的任何位置加载资源 [英] Load resource from anywhere in classpath
问题描述
我有一个简单的java应用程序,它从当前包中加载属性文件。
I have a simple java application that loads a properties file from the current package.
this.getClass().getResourceAsStream("props.properties");
当我想要的属性文件在当前包中时,这可以正常工作。但是,我想将此应用程序打包为JAR,并使用我使用它的新属性文件定义和覆盖。有没有办法加载类路径上名为props.properties的第一个资源?
This works fine when the property file I want is in the current package. However, I want to package this application as a JAR and define and override with a new properties file where I use it. Is there a way to load the first resource named "props.properties" that is on the classpath?
我希望通过命令轻松覆盖属性文件line:
I want it to be as easy to override the properties file via command line:
java.exe -classpath props.properties;myJar.jar com.test.MyApp
我不想解压缩JAR并修改属性文件来改变一些东西。我觉得我错过了一些明显的东西...
I don't want to have to unpack the JAR and modify the properties file to change something. I feel like I'm missing something obvious...
推荐答案
如果一切都失败了你可以使用两个不同的文件名,比如说 props-default.properties
在 myJar.jar
和 props.properties $ c $里面c>在命令行上覆盖。在您的代码中,您首先尝试加载
props.properties
文件,然后回退到 props-default.properties
if它没有找到。
If all else fails you could use two different file names, say props-default.properties
inside myJar.jar
and props.properties
to override on the command-line. In your code, you'd try loading the props.properties
file first and fallback to props-default.properties
if it wasn't found.
这篇关于从类路径中的任何位置加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!