如何使用编程方式创建JAR文件java.util.jar.JarOutputStream?我的程序生成的JAR文件看起来是正确的(它提取正常)但是当我尝试从中加载库时,Java抱怨它无法找到明确存储在其中的文件.如果我提取JAR文件并使用Sun的jar命令行工具重新压缩它,则生成的库可以正常工作.简而言之,我的JAR文件有问题.
请解释如何以编程方式创建JAR文件,并使用清单文件.
我试图从JarInputStream读取文件,文件的大小返回-1.我正在从URL访问Jar文件作为inputStream.
URLConnection con = url.openConnection();
JarInputStream jis = new JarInputStream(con.getInputStream());
JarEntry je = null;
while ((je = jis.getNextJarEntry()) != null) {
htSizes.put(je.getName(), new Integer((int) je.getSize()));
if (je.isDirectory()) {
continue;
}
int size = (int) je.getSize();
// -1 means unknown size.
if (size == -1) {
size = ((Integer) htSizes.get(je.getName())).intValue();
}
byte[] b = new byte[(int) size];
int rb = 0;
int chunk = 0;
while (((int) size - rb) > 0) {
chunk = jis.read(b, rb, (int) size - rb); …Run Code Online (Sandbox Code Playgroud)