无法从 JAR 加载属性
几天来,我一直在尝试让我的 Java 项目从位于其 JAR 文件中的文件加载一些属性。但是,当我尝试加载文件时,我不断收到空指针。
文件夹层次结构的属性文件位于 /data 中,所有源文件位于 /emp/**/** ...
代码
Properties defaultProps = new Properties();
try {
InputStream in = getClass().getClassLoader().getResourceAsStream("data/build_info.properties");
//InputStream in = new URL("file:data/build_info.properties").openStream();
defaultProps.load(in);
in.close();
} catch (FileNotFoundException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
} catch (NullPointerException e){
Log.E("NPE- Properties not loaded", "properties");
revision = "Properties file not found";
}
if (defaultProps.getProperty("build.major.number") == null) {
Log.W("Properties not loaded", "properties");
revision = "Properties file not found";
} else {
Log.V("Properties Loaded Successfully", "properties");
revision = "Version: " + defaultProps.getProperty("build.major.number")
+ "." + defaultProps.getProperty("build.minor.number") + " "
+ "Revision: "
+ defaultProps.getProperty("build.revision.number");
}
For a few days now, I have been trying to get my Java project to load some properties from a file located in it's JAR file. However, I am constantly getting a null pointer when trying to load in the file.
The folder hierarchy has the properties file in /data, and all the source files in /emp/**/** ...
Code
Properties defaultProps = new Properties();
try {
InputStream in = getClass().getClassLoader().getResourceAsStream("data/build_info.properties");
//InputStream in = new URL("file:data/build_info.properties").openStream();
defaultProps.load(in);
in.close();
} catch (FileNotFoundException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
} catch (NullPointerException e){
Log.E("NPE- Properties not loaded", "properties");
revision = "Properties file not found";
}
if (defaultProps.getProperty("build.major.number") == null) {
Log.W("Properties not loaded", "properties");
revision = "Properties file not found";
} else {
Log.V("Properties Loaded Successfully", "properties");
revision = "Version: " + defaultProps.getProperty("build.major.number")
+ "." + defaultProps.getProperty("build.minor.number") + " "
+ "Revision: "
+ defaultProps.getProperty("build.revision.number");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
data
位于 jar 的根目录中,并且build_info.properties
位于 jar 的数据目录中,并且如果 jar 位于类路径上,则getClass().getClassLoader().getResourceAsStream("data/build_info.properties");
将找到该属性文件。您还可以使用 getClass().getResourceAsStream("/data/build_info.properties");。如果 getClass() 返回的类加载器加载的类与类路径中包含您的 jar 的类加载器不同,则可能会出现特殊情况。
If
data
is in the root of your jar, and ifbuild_info.properties
is inside the data directory in the jar, and if the jar is on the classpath, thengetClass().getClassLoader().getResourceAsStream("data/build_info.properties");
will find that properties file. You could also usegetClass().getResourceAsStream("/data/build_info.properties");
.Peculiarities can arise if
getClass()
returns a class loaded by a classloader different than the one that has your jar on its classpath.你也可以尝试——
You could alternatively try --
我对一个非常简单的控制台应用程序也有同样的问题。最终我在 https://stackoverflow.com/a/1464541/1792291 找到了提示,并将我的控制台应用程序变成一个 Swing 应用程序,突然一切都正常了。
上面链接中的解释确实有一定道理:由于控制台应用程序在创建 shell 时获取其属性(包括 CLASSPATH),因此它不会知道 JVM 期间/为/由 JVM 定义的类路径。
I had the same issue with a dead-simple console application. Eventually I found a hint at https://stackoverflow.com/a/1464541/1792291 and I made my console app into a swing app, and suddenly everything worked.
The explanation in the link above does make some sense: since the console app gets its properties (including CLASSPATH) once when the shell is created, it won't know about the classpath defined during/for/by the JVM.