eclipse:在 jar 导出中包含任意文件

发布于 2024-12-26 15:23:55 字数 1330 浏览 4 评论 0原文

我有一个包含任意文件的目录,我想将其包含在我的 jar 中 - 但是,我无法找到一种与导出 -> 一起使用的方法。 “可运行的罐子”。我尝试过将目录设为“源路径”的技巧,但当我构建 jar 时它仍然不存在。我意识到我可以手动将它们添加到 jar 中(毕竟它只是一个 zip) - 或者我可以使用 ant 脚本或其他构建系统 - 但我正在寻找适用于 out-of-the- 的东西框 Eclipse“Java 项目”。

这是一个例子。我想尝试加载 log4j.properties(如果存在)。如果没有,我想从我的 jar 文件中包含的“默认”中写出它。最后,如果失败,它会加载默认值。

请注意,我还不知道下面的代码是否有效,它可能需要调整。我并不是在寻求帮助,我只是提供我想做的事情的背景。

        // initialize logging libraries
    File log4jFile = new File("log4j.properties");
    if (log4jFile.exists() & log4jFile.canRead()) {
        PropertyConfigurator.configure(log4jFile.getAbsolutePath());
    }
    else {
        try {
            InputStream log4jJarstream = Main.class.getResourceAsStream(sepd + "resources" + sep + "log4j.properties");
            OutputStream outStream = new FileOutputStream(new File("log4j.properties"));
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = log4jJarstream.read(bytes)) != -1) {
                outStream.write(bytes, 0, read);
            }
            log4jJarstream.close();
            outStream.flush();
            outStream.close();
        }
        catch (Exception e) {
            BasicConfigurator.configure();
            log.warn("Error writing log4j.properties, falling back to defaults.");
        }
    }

I have a directory of arbitrary files I want to include in my jar - however, I cannot figure out a way to do so that works with export -> "Runnable jar". I've tried the trick of making the directory a 'source path' but it is still absent when I build the jar. I realize I can manually add them into the jar (it is just a zip, after all) - or I could use an ant script or other build system - but I'm looking for something that works for an out-of-the-box Eclipse "Java project".

Here's an example. I want to try to load log4j.properties if it exists. If not, I want to write it out from an included 'default' in my jarfile. Finally, it loads the defaults if that fails.

Note that I have no idea yet if the code below works, it will likely need tweaking. I'm not asking for help with that, I'm just giving context for what I want to do.

        // initialize logging libraries
    File log4jFile = new File("log4j.properties");
    if (log4jFile.exists() & log4jFile.canRead()) {
        PropertyConfigurator.configure(log4jFile.getAbsolutePath());
    }
    else {
        try {
            InputStream log4jJarstream = Main.class.getResourceAsStream(sepd + "resources" + sep + "log4j.properties");
            OutputStream outStream = new FileOutputStream(new File("log4j.properties"));
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = log4jJarstream.read(bytes)) != -1) {
                outStream.write(bytes, 0, read);
            }
            log4jJarstream.close();
            outStream.flush();
            outStream.close();
        }
        catch (Exception e) {
            BasicConfigurator.configure();
            log.warn("Error writing log4j.properties, falling back to defaults.");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

绿光 2025-01-02 15:23:55

我将 log4j.properties 添加到我的 src 文件夹中,并将 jar 导出为可运行文件。它起作用了。

I added the log4j.properties to my src folder, and exported the jar as a runnable. It worked.

你的背包 2025-01-02 15:23:55

只需尝试导出 --> JAR 文件而不是导出可运行的 JAR 文件:它允许您选择多个资源以包含在生成的存档中。

您还可以指定 Main-Class 属性,就像后一个选项一样。

顺便说一句,如果您使用某种构建工具(例如 Ant < jar> targetMaven Jar 插件 )。如果您使用 Eclipse 生成 JAR 文件,还可以选择保存 Ant 构建文件,以便稍后为您完成该任务。

Simply try Export --> JAR file instead of exporting Runnable JAR file: it allows you to select multiple resources to include in the generated archive.

You can also specify the Main-Class property, just like with the latter option.

BTW it is more convenient if you use some kind of build tool (like Ant <jar> target or the Maven Jar plugin). If you use Eclipse to generate the JAR file, there is also an option to save an Ant buildfile that can do the task for you later on.

爱你不解释 2025-01-02 15:23:55

将文件作为资源加载的代码中存在错误...Eclipse 似乎“看到”了该错误,因此拒绝打包该文件。我将该文件放在类文件旁边,并更改了搜索该文件的方式,并将其与 .class 文件打包在一起,并且能够在执行期间读取。新的代码片段:

// initialize logging libraries
    File log4jFile = new File("log4j.properties");
    if (log4jFile.exists() & log4jFile.canRead()) {
        PropertyConfigurator.configure("log4j.properties");
    }
    else {
        try {
            InputStream log4jJarstream = Main.class.getResourceAsStream("log4j.properties");
            OutputStream outStream = new FileOutputStream(new File("log4j.properties"));
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = log4jJarstream.read(bytes)) != -1) {
                outStream.write(bytes, 0, read);
            }
            log4jJarstream.close();
            outStream.flush();
            outStream.close();

            PropertyConfigurator.configure("log4j.properties");
        }
        catch (Exception e) {
            BasicConfigurator.configure();
            log.warn("Error writing log4j.properties, falling back to defaults.");
            log.warn(e);
            log.warn("STACK TRACE:");
            int i = 0;
            StackTraceElement[] trace = e.getStackTrace();
            while (i < trace.length) {
                log.warn(trace[i]);
                i++;
            }
        }
    }

There was an error in the code loading the file as a resource... which it seems Eclipse "saw" and as such was refusing to package the file. I put the file along side the class files and altered the way I search for the file, and it packaged it with the .class files and was able to be read during execution. New code snippet:

// initialize logging libraries
    File log4jFile = new File("log4j.properties");
    if (log4jFile.exists() & log4jFile.canRead()) {
        PropertyConfigurator.configure("log4j.properties");
    }
    else {
        try {
            InputStream log4jJarstream = Main.class.getResourceAsStream("log4j.properties");
            OutputStream outStream = new FileOutputStream(new File("log4j.properties"));
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = log4jJarstream.read(bytes)) != -1) {
                outStream.write(bytes, 0, read);
            }
            log4jJarstream.close();
            outStream.flush();
            outStream.close();

            PropertyConfigurator.configure("log4j.properties");
        }
        catch (Exception e) {
            BasicConfigurator.configure();
            log.warn("Error writing log4j.properties, falling back to defaults.");
            log.warn(e);
            log.warn("STACK TRACE:");
            int i = 0;
            StackTraceElement[] trace = e.getStackTrace();
            while (i < trace.length) {
                log.warn(trace[i]);
                i++;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文