簡體   English   中英

如何只包裝包文件夾和它下面的類。

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

我通過查看Web上的示例創建了一個實用程序類來構建jar文件。 當我給出源文件夾和輸出jar名稱時,該類創建jar文件。 問題是當我展開jar時,我看到.class文件的絕對路徑,而不是只包含源文件夾。 如何僅包含源文件夾的內容

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

當我將source作為/ tmp / example / package時,jar包含/tmp/example/package/com/example/java/HellWorld.class而不僅僅是com / example / java / HellWorld.class

這是我的代碼

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已經具備了你想要實現的功能。 只需下載jdk並使用它:)

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

使用另一個目錄中的所有文件創建jar:

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

結果是

$ 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

使用單個類創建jar:

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

結果是

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

另一種方法。 在類JarUtil中添加新的靜態字段,或將其作為新參數傳遞給add方法

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

在方法中add

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

自java 7起必須工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM