我尝试从正在运行的 jar 中复制 jar 文件,可以在 eclipse IDE 中运行,但在外部运行 jar 应用程序时不行。这里的部分代码:
AWdir = new File("C:\\Windows\\Temp\\aw\\");
AWdir.mkdir();
if(AWdir!=null && !AWdir.isDirectory()){
MessageDialog.openError(getShell(), "Error create directry", "DIR:"+AWdir);
}
String resource = "generate.jar";
URL res = MainWindow.this.getClass().getResource(resource);
fileJar = new File(res.getFile());
JarFile jarFile=new JarFile(fileJar);
String fileName = jarFile.getName();
String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
File destFile = new File(AWdir, fileNameLastPart);
JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
InputStream is = jarFile.getInputStream(entry);
//jos.putNextEntry(entry);
//create a new entry to avoid ZipException: invalid entry compressed size
jos.putNextEntry(new JarEntry(entry.getName()));
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
is.close();
jos.flush();
jos.closeEntry();
}
jos.close();
destFile.createNewFile();
请帮助并感谢您提出解决上述问题的建议