无法从 JAR 加载属性

发布于 2024-11-27 13:53:39 字数 1247 浏览 1 评论 0原文

几天来,我一直在尝试让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懒猫 2024-12-04 13:53:39

如果 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 if build_info.properties is inside the data directory in the jar, and if the jar is on the classpath, then getClass().getClassLoader().getResourceAsStream("data/build_info.properties"); will find that properties file. You could also use getClass().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.

婴鹅 2024-12-04 13:53:39

你也可以尝试——

Thread.currentThread().getContextClassLoader().getResourceAsStream("data/build_info.properties");

You could alternatively try --

Thread.currentThread().getContextClassLoader().getResourceAsStream("data/build_info.properties");
乙白 2024-12-04 13:53:39

我对一个非常简单的控制台应用程序也有同样的问题。最终我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文