简体   繁体   English

如何只包装包文件夹和它下面的类。

[英]How to jar only the package folder and the class under it .

I created a utility class to build the jar file by looking at the examples from the web. 我通过查看Web上的示例创建了一个实用程序类来构建jar文件。 The class creates the jar file when I give the source folder and output jar name. 当我给出源文件夹和输出jar名称时,该类创建jar文件。 The issue is when I expand the jar , I see the absolute path with .class file, instead of just contains of the source folder. 问题是当我展开jar时,我看到.class文件的绝对路径,而不是只包含源文件夹。 How do I just include only the contents of the source folder 如何仅包含源文件夹的内容

For example , in /tmp/example/package, I have com/example/java/HellWorld.class. 例如,在/ tmp / example / package中,我有com / example / java / HellWorld.class。

When I give source as /tmp/example/package, the jar contains /tmp/example/package/com/example/java/HellWorld.class instead of just com/example/java/HellWorld.class 当我将source作为/ tmp / example / package时,jar包含/tmp/example/package/com/example/java/HellWorld.class而不仅仅是com / example / java / HellWorld.class

Here is my code 这是我的代码

public final class JarUtil {

private static Logger logger = LoggerFactory.getLogger(JarUtil.class);

private JarUtil() {

}

/**
 * @param dirToBeJared
 * @param outputJarFileName
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void createJar(String dirToBeJared, String outputJarFileName) {

    logger.info("into create jar dirToBeJared: " + ", outputJarFileName" + outputJarFileName);

    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    JarOutputStream target = null;

    try {

        target = new JarOutputStream(new FileOutputStream(outputJarFileName), manifest);

    } catch (FileNotFoundException e) {
        logger.error("error during create jar:" + e);
    } catch (IOException e) {
        logger.error("error during create jar:" + e);
    }

    try {

        add(new File(dirToBeJared), target);

    } catch (IOException e) {
        logger.error("error during create jar:" + e);
    }

    try {
        target.close();
    } catch (IOException e) {
        logger.error("error during create jar:" + e);
    }
}

private static void add(File source, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {
        if (source.isDirectory()) {
            String name = source.getPath().replace("\\", "/");
            if (!name.isEmpty()) {
                if (!name.endsWith("/")) {
                    name += "/";
                }

              //  JarEntry entry = new JarEntry("com/athena");
                JarEntry entry = new JarEntry(name);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles()) {
                add(nestedFile, target);
            }    
            return;
        }

        JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);

        try {
            in = new BufferedInputStream(new FileInputStream(source));
        } catch (FileNotFoundException e) {
            logger.error("error during the creating the jar: " + e);
        }

        byte[] buffer = new byte[1024];
        while (true) {
            int count = in.read(buffer);
            if (count == -1) {
                break;
            }    
            target.write(buffer, 0, count);
        }
        target.closeEntry();
    } finally {
        if (in != null) {
            in.close();
        }    
    }
}

public static void main(String[] args) {

    JarUtil.createJar("/tmp/examples/package","HelloWorld.jar");

}

} }

jar already has functionality you want to realize. jar已经具备了你想要实现的功能。 Just download jdk and use it :) 只需下载jdk并使用它:)

Show files in tmp/example/package tmp/example/package显示文件

$ find tmp/example/package/ -type f
tmp/example/package/com/example/java/Hello.class
tmp/example/package/com/example/java/HellWorld.class

Create jar with all files in another directory: 使用另一个目录中的所有文件创建jar:

$ jar -cf new1.jar -C tmp/example/package/ .

Result is 结果是

$ jar -tf new1.jar 
META-INF/
META-INF/MANIFEST.MF
com/
com/example/
com/example/java/
com/example/java/Hello.class
com/example/java/HellWorld.class

Create jar with single class: 使用单个类创建jar:

$ jar -cf new2.jar -C tmp/example/package com/example/java/HellWorld.class

Result is 结果是

$ jar -tf new2.jar 
META-INF/
META-INF/MANIFEST.MF
com/example/java/HellWorld.class

Another solution. 另一种方法。 Add new static field in class JarUtil, or transmit it as new argument to add method 在类JarUtil中添加新的静态字段,或将其作为新参数传递给add方法

Path startDir = Paths.get("/tmp/example/package").toAbsolutePath();

in method add : 在方法中add

target.putNextEntry(new ZipEntry(
     startDir.relativize(source.toPath.toAbsolutePath()).
     toString()));

Must work since java 7. 自java 7起必须工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM