【问题标题】:eclipse: include abitrary files in jar exporteclipse:在jar导出中包含任意文件
【发布时间】:2012-02-09 23:40:34
【问题描述】:

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

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

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

        // 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.");
        }
    }

【问题讨论】:

    标签: java eclipse build jar executable-jar


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 我已经找到了解决方案,但还不能接受。请参阅我自己对我的问题的回答。
      【解决方案3】:

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

      // 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++;
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2015-07-03
        • 2014-10-20
        • 2015-02-01
        • 1970-01-01
        • 2013-03-08
        • 2020-02-16
        • 1970-01-01
        • 2017-07-31
        • 1970-01-01
        相关资源
        最近更新 更多