Java 如何将文件添加到正在运行的jar
这是我的代码,我正在寻找一种将文件添加到运行jar的方法。提前谢谢。当前发生的错误是“无效的条目大小(预期为62,但得到0字节)。62字节是我的清单文件的大小,它将被写入。我不确定这与任何事情有什么关系Java 如何将文件添加到正在运行的jar,java,file,jar,zip,Java,File,Jar,Zip,这是我的代码,我正在寻找一种将文件添加到运行jar的方法。提前谢谢。当前发生的错误是“无效的条目大小(预期为62,但得到0字节)。62字节是我的清单文件的大小,它将被写入。我不确定这与任何事情有什么关系 JarFile replace = new JarFile("newgame.jar"); JarInputStream jis = null; JarOutputStream jos = new JarOutputStream(new FileO
JarFile replace = new JarFile("newgame.jar");
JarInputStream jis = null;
JarOutputStream jos = new JarOutputStream(new FileOutputStream(
Launcher.class.getProtectionDomain().getCodeSource().getLocation().getPath()));
for (Enumeration<JarEntry> list = replace.entries(); list.hasMoreElements();) {
JarEntry nextEntry = list.nextElement();
if (!nextEntry.getName().equals("Launcher.class")) {
jos.putNextEntry(nextEntry);
jis = new JarInputStream(replace.getInputStream(nextEntry));
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] byteBuff = new byte[1024];
int bytesRead = 0;
while ((bytesRead = jis.read(byteBuff)) != -1)
out.write(byteBuff, 0, bytesRead);
jos.write(out.toByteArray());
out.close();
jis.close();
}
}
replace.close();
jos.close();
new File("newgame.jar").delete();
JarFile replace=newjarfile(“newgame.jar”);
JarInputStream jis=null;
JarOutputStream jos=新的JarOutputStream(新文件输出流(
Launcher.class.getProtectionDomain().getCodeSource().getLocation().getPath());
对于(枚举列表=replace.entries();list.hasMoreElements();){
JarEntry nextEntry=list.nextElement();
如果(!nextEntry.getName().equals(“Launcher.class”)){
何塞·普特内森特里(下森特里);
jis=新的JarInputStream(replace.getInputStream(nextEntry));
ByteArrayOutputStream out=新建ByteArrayOutputStream();
byte[]byteBuff=新字节[1024];
int字节读取=0;
while((bytesRead=jis.read(byteBuff))!=-1)
out.write(byteBuff,0,bytesRead);
jos.write(out.toByteArray());
out.close();
jis.close();
}
}
replace.close();
jos.close();
新文件(“newgame.jar”).delete();
用
JarFile replace = new JarFile("newgame.jar");
InputStream is = null;
JarOutputStream jos = new JarOutputStream(new FileOutputStream(
Launcher.class.getProtectionDomain().getCodeSource().getLocation().getPath()));
for (Enumeration<JarEntry> list = replace.entries(); list.hasMoreElements();) {
JarEntry nextEntry = list.nextElement();
jos.putNextEntry(nextEntry);
is = replace.getInputStream(nextEntry);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] byteBuff = new byte[(int) nextEntry.getSize()];
int bytesRead = 0;
while ((bytesRead = is.read(byteBuff, 0, byteBuff.length)) != -1)
out.write(byteBuff, 0, bytesRead);
jos.write(out.toByteArray());
out.close();
is.close();
}
replace.close();
jos.close();
new File("newgame.jar").delete();
JarFile replace=newjarfile(“newgame.jar”);
InputStream=null;
JarOutputStream jos=新的JarOutputStream(新文件输出流(
Launcher.class.getProtectionDomain().getCodeSource().getLocation().getPath());
对于(枚举列表=replace.entries();list.hasMoreElements();){
JarEntry nextEntry=list.nextElement();
何塞·普特内森特里(下森特里);
is=replace.getInputStream(nextEntry);
ByteArrayOutputStream out=新建ByteArrayOutputStream();
byte[]byteBuff=新字节[(int)nextEntry.getSize()];
int字节读取=0;
while((bytesRead=is.read(byteBuff,0,byteBuff.length))!=-1)
out.write(byteBuff,0,bytesRead);
jos.write(out.toByteArray());
out.close();
is.close();
}
replace.close();
jos.close();
新文件(“newgame.jar”).delete();
我将JarInputStreams转换为InputStreams,出于某种原因,这起到了作用。为什么要这样做?这可能是一个问题。代码中的哪个语句给了您错误?@JimGarrison the jos.putnextry(nextery);给了我错误。