如何将包目录结构添加到我的jar中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将包目录结构添加到我的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);
答案
对于对未来感兴趣的任何人,我通过使用以下代码解决了这个问题:
out.putNextEntry(new ZipEntry("io/ironbytes/corkscrew/client/ClientInitiator.class"));
来自new ZipEntry
的import java.util.zip.ZipEntry;
类将允许您同时在jar中包含您的类和目录结构。
以上是关于如何将包目录结构添加到我的jar中的主要内容,如果未能解决你的问题,请参考以下文章
如何在没有 Cocoapods 的情况下手动将包添加到 Swift