繁体   English   中英

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

[英]eclipse: include abitrary files in jar export

我有一个要包含在jar中的任意文件目录-但是,我无法找到一种可以与export->“ Runnable jar”一起使用的方法。 我尝试了使目录成为“源路径”的技巧,但是在构建jar时仍然不存在。 我意识到我可以手动将它们添加到jar中(毕竟它只是一个zip)-或者我可以使用ant脚本或其他构建系统-但我正在寻找一种适用于非常规的东西框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.");
        }
    }

我将log4j.properties添加到我的src文件夹中,并将jar导出为可运行的。 有效。

只需尝试导出-> JAR文件,而不是导出Runnable JAR文件 :它允许您选择多个资源以包括在生成的归档文件中。

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

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

在将文件作为资源加载的代码中有一个错误……这似乎是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++;
            }
        }
    }

暂无
暂无

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

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