如何将包目录结构添加到我的jar中

时间:2018-01-02 20:05:02

标签: java jar

我正在尝试动态编译java文件并将它们添加到我的jar中然后可以执行。我可以编译并创建jar,但问题是尝试在jar中包含包的目录结构。我的.class文件放在jar文件的根目录下,但不会运行,因为它们需要在正确的包/目录结构中。

简而言之,我有:

JAR.jar / classHere.class

我需要:

JAR.jar / IO / ironbytes /螺丝起子/客户端/ classHere.class

我正在使用此代码来创建jar。

package io.ironbytes.corkscrew.models;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class JarFactory {
    public static int BUFFER_SIZE = 10240;

    public void compile() {
        // Compile source file.
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        compiler.run(null, null, null, "src/io/ironbytes/corkscrew/client/ClientInitiator.java");
        compiler.run(null, null, null, "src/io/ironbytes/corkscrew/client/ScreenViewer.java");
        compiler.run(null, null, null, "src/io/ironbytes/corkscrew/client/EnumCommands.java");
        compiler.run(null, null, null, "src/io/ironbytes/corkscrew/client/ServerDelegate.java");
    }

    public void createJarArchive(File archiveFile, File[] tobeJared) {
        try {
            byte buffer[] = new byte[BUFFER_SIZE];
            // Open archive file
            FileOutputStream stream = new FileOutputStream(archiveFile);

            Manifest man = new Manifest();
            man.getMainAttributes().putValue( "Manifest-Version", "1.0" );
            man.getMainAttributes().putValue( "Class-Path", "." );
            man.getMainAttributes().putValue( "Main-Class", "ClientInitiator");

            JarOutputStream out = new JarOutputStream(stream, man);

            for (int i = 0; i < tobeJared.length; i++) {
                if (tobeJared[i] == null || !tobeJared[i].exists()
                        || tobeJared[i].isDirectory())
                    continue; // Just in case...
                System.out.println("Adding " + tobeJared[i].getName());

                // Add archive entry
                JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
                jarAdd.setTime(tobeJared[i].lastModified());
                out.putNextEntry(jarAdd);

                // Write file to archive
                FileInputStream in = new FileInputStream(tobeJared[i]);
                while (true) {
                    int nRead = in.read(buffer, 0, buffer.length);
                    if (nRead <= 0)
                        break;
                    out.write(buffer, 0, nRead);
                }
                in.close();
            }

            out.close();
            stream.close();
            System.out.println("Adding completed OK");
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Error: " + ex.getMessage());
        }
    }
}

我按照以下方式传递参数:

File[] arr = {new File("src/io/ironbytes/corkscrew/client/ClientInitiator.class") 
                            , new File("src/io/ironbytes/corkscrew/client/ClientInitiator$1.class") 
                            , new File("src/io/ironbytes/corkscrew/client/ScreenViewer.class")
                            , new File("src/io/ironbytes/corkscrew/client/EnumCommands.class")
                            , new File("src/io/ironbytes/corkscrew/client/ServerDelegate.class")
                            };

                    JarFactory jar = new JarFactory();
                    jar.compile();
                    jar.createJarArchive(new File("/Users/Bob/Desktop/he.jar"), arr);

1 个答案:

答案 0 :(得分:1)

对于对未来感兴趣的任何人,我使用以下代码解决了这个问题:

out.putNextEntry(new ZipEntry("io/ironbytes/corkscrew/client/ClientInitiator.class"));

来自new ZipEntry的{​​{1}}类允许您同时在jar中包含您的类和目录结构。