English 中文(简体)
缩略语:在杰尔出口中包含一个借方档案
原标题:eclipse: include abitrary files in jar export

我有一份专断档案目录,我想在我的jar中列入——然而,我不能说出这样做的方法,以便与出口——和“可兰经”合作。 我曾尝试过使名录成为一种源路的trick,但在我建造jar时仍然没有。 我认识到,我可以人工将其添加到(这只是一纸空文,毕竟是一纸空文)——或者我可以使用一纸script或其他建筑系统——但我却想想出一些东西,为“Java项目”设计出一纸。

这里就是一个例子。 如果存在的话,我就试图装上记录4j.properties。 如果不是的话,我想从我的jar档案中包含的缺省中写出来。 最后,如果违约的话,它就会装上违约。

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.");
        }
    }
最佳回答

法典中将档案装成一种资源......似乎Eclipse“saw”因而拒绝包装档案。 我将档案放在同级档案的一边,并改变了我搜索档案的方式,把档案包上了.子档案,在处决期间可以阅读。 新法典:

// 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++;
            }
        }
    }
问题回答

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

Simplytries Export --> JAR file und自出口Runnable JAR file:允许你选择多种资源,以列入生成的档案。

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

BTW 如果你使用某种建筑工具(如Ant <jar> Target





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签