从Jar

时间:2016-04-13 10:23:30

标签: java eclipse jar executable-jar

我正在尝试从jar内的某些类和txt文件创建jar文件。它是一个生成客户端的简单服务器程序。

如果我使用WinRAR检查它,我想要访问的文件夹确实在我的jar文件中。我已经制作了一个适用于IDE (Eclipse)的方法。如果我在IDE中运行我的程序,我就能生成一个执行的jar。

我知道我无法访问jar文件中的文件作为文件。但是我的方法使用文件的路径。

此方法创建清单文件没有任何问题:

public void createClientJar(String path, String fileName, String hostAddress, String portNumber, String connectionInterval) {
    try {
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, ".");
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "client.utils.Client");

        JarOutputStream target = new JarOutputStream(new FileOutputStream(path + "/" + fileName + ".jar"), manifest);

        File serverRoot = new File(getClass().getResource("/client").getPath());

        addSourceToJar(serverRoot, target, "", hostAddress, portNumber, connectionInterval);

        target.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是生成jar文件的方法:

private void addSourceToJar(File source, JarOutputStream target, String folder, String hostAddress, String portNumber, String connectionInterval) {
    BufferedInputStream in = null;
    File entryFile = null;
    JarEntry entry = null;
    String path = source.getPath().replace("\\", "/");
    String previousFolder = folder;

    try {
        if (source.isDirectory()) {
            if (!path.isEmpty()) {
                if (!path.endsWith("/"))
                    path += "/";

                if (!path.endsWith("bin/")) {
                    entryFile = new File(path);
                    entry = new JarEntry(previousFolder + entryFile.getName() + "/");
                    previousFolder += entryFile.getName() + "/";

                    target.putNextEntry(entry);
                    target.closeEntry();
                }
            }
            for (File nestedFile : source.listFiles()) {
                addSourceToJar(nestedFile, target, previousFolder, hostAddress, portNumber, connectionInterval);
            }
            return;
        }

        entryFile = new File(path);
        entry = new JarEntry(previousFolder + entryFile.getName());
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);

        if(entry.getName().contains("data")){
            // Replaces connection data in data.txt
            replaceData(path, hostAddress, portNumber, connectionInterval);
        }

        in = new BufferedInputStream(new FileInputStream(path));
        byte[] buffer = new byte[1024];
        int length;

        while ((length = in.read(buffer)) != -1) {
            target.write(buffer, 0, length);
        }
        target.closeEntry();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

问题是我得到Nullpointer例外:

java.lang.NullPointerException
        at model.Model.createClientJar(Model.java:204)

这一行:

File serverRoot = new File(getClass().getResource("/client").getPath());

此外我认为这不是要走的路。因为我无法直接访问Jar中的文件。

我无法提出改变我的方法的逻辑,因此它在我的IDE和导出为Runnable Jar时都有效。

我希望有人能指出我在addSourceToJar()方法中哪些行会失败的正确方向。

提前致谢

0 个答案:

没有答案
相关问题