Java eclipse:在jar导出中包含abitrary文件

Java eclipse:在jar导出中包含abitrary文件,java,eclipse,build,jar,executable-jar,Java,Eclipse,Build,Jar,Executable Jar,我有一个任意文件的目录,我想包含在我的jar中——但是,我无法找到一种方法来实现导出->Runnable jar。我尝试过将目录设置为“源路径”的技巧,但在构建jar时,它仍然不存在。我意识到我可以手动将它们添加到jar中(毕竟这只是一个zip文件)——或者我可以使用ant脚本或其他构建系统——但我正在寻找一种适用于现成Eclipse“Java项目”的东西 这里有一个例子。如果log4j.properties存在,我想尝试加载它。如果没有,我想从JAR文件中包含的“默认值”中写出它。最后,如果失

我有一个任意文件的目录,我想包含在我的jar中——但是,我无法找到一种方法来实现导出->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.");
        }
    }
只需尝试导出-->JAR文件而不是导出可运行的JAR文件:它允许您选择要包含在生成的存档中的多个资源

您还可以指定主类属性,就像使用后一个选项一样

顺便说一句,如果您使用某种构建工具(如或)。如果您使用Eclipse生成JAR文件,还有一个保存Ant构建文件的选项,可以在以后为您执行此任务。

只需尝试导出-->JAR文件,而不是导出可运行的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++;
            }
        }
    }
//初始化日志库
文件log4jFile=新文件(“log4j.properties”);
if(log4jFile.exists()&log4jFile.canRead()){
PropertyConfigurator.configure(“log4j.properties”);
}
否则{
试一试{
InputStream log4jJarstream=Main.class.getResourceAsStream(“log4j.properties”);
OutputStream outStream=新文件OutputStream(新文件(“log4j.properties”);
int read=0;
字节[]字节=新字节[1024];
而((read=log4jJarstream.read(bytes))!=-1){
超流写入(字节,0,读取);
}
log4jJarstream.close();
冲水;
exptream.close();
PropertyConfigurator.configure(“log4j.properties”);
}
捕获(例外e){
BasicConfigurator.configure();
warn(“写入log4j.properties时出错,返回默认值”);
log.warn(e);
log.warn(“堆栈跟踪:”);
int i=0;
StackTraceElement[]trace=e.getStackTrace();
而(i
将文件作为资源加载的代码中出现错误。。。这似乎是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++;
            }
        }
    }
//初始化日志库
文件log4jFile=新文件(“log4j.properties”);
if(log4jFile.exists()&log4jFile.canRead()){
PropertyConfigurator.configure(“log4j.properties”);
}
否则{
试一试{
InputStream log4jJarstream=Main.class.getResourceAsStream(“log4j.properties”);
OutputStream outStream=新文件OutputStream(新文件(“log4j.properties”);
int read=0;
字节[]字节=新字节[1024];
而((read=log4jJarstream.read(bytes))!=-1){
超流写入(字节,0,读取);
}
log4jJarstream.close();
冲水;
exptream.close();
PropertyConfigurator.configure(“log4j.properties”);
}
捕获(例外e){
BasicConfigurator.configure();
warn(“写入log4j.properties时出错,返回默认值”);
log.warn(e);
log.warn(“堆栈跟踪:”);
int i=0;
StackTraceElement[]trace=e.getStackTrace();
而(i
我将log4j.properties添加到src文件夹中,并将jar作为可运行文件导出。它成功了。

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

我已经找到了解决方案,但还不能接受。看看我自己对问题的回答。我已经找到了解决办法,但还不能接受。看看我自己对我的问题的回答。