解決jar包rar壓縮后無法運行問題
jar包rar壓縮后無法運行
報錯為Failed to get nested archive for entry
java.io.IOException: Unable to open nested jar file
It has been compressed and nested jar files must be stored without compression,Please check the mechanism used to create your executable jar file
如果有出現(xiàn)該問題,請檢查jar包壓縮時是否用壓縮軟件方式打開。
如果丟進去的是第三方j(luò)ar包或者更新后的引入lib包,復(fù)制到壓縮目錄時需要選擇壓縮方式,一定要選擇存儲方式。
不然rar默認會給你再次壓縮,部署到linux后項目就無法正常運行。
再次壓縮后大小與未壓縮時不一致,所以會報錯。
一定要選擇存儲方式復(fù)制或覆蓋到壓縮目錄
java解壓縮zip和rar的工具類
import java.io.File; import java.io.FileOutputStream; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Expand; import de.innosystec.unrar.Archive; import de.innosystec.unrar.rarfile.FileHeader; public class DeCompressUtil { /** * 解壓zip格式壓縮包 * 對應(yīng)的是ant.jar */ private static void unzip(String sourceZip,String destDir) throws Exception{ try{ Project p = new Project(); Expand e = new Expand(); e.setProject(p); e.setSrc(new File(sourceZip)); e.setOverwrite(false); e.setDest(new File(destDir)); /* ant下的zip工具默認壓縮編碼為UTF-8編碼, 而winRAR軟件壓縮是用的windows默認的GBK或者GB2312編碼 所以解壓縮時要制定編碼格式 */ e.setEncoding("gbk"); e.execute(); }catch(Exception e){ throw e; } } /** * 解壓rar格式壓縮包。 * 對應(yīng)的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又會用到commons-logging-1.1.1.jar */ private static void unrar(String sourceRar,String destDir) throws Exception{ Archive a = null; FileOutputStream fos = null; try{ a = new Archive(new File(sourceRar)); FileHeader fh = a.nextFileHeader(); while(fh!=null){ if(!fh.isDirectory()){ //1 根據(jù)不同的操作系統(tǒng)拿到相應(yīng)的 destDirName 和 destFileName String compressFileName = fh.getFileNameString().trim(); String destFileName = ""; String destDirName = ""; //非windows系統(tǒng) if(File.separator.equals("/")){ destFileName = destDir + compressFileName.replaceAll("\\\\", "/"); destDirName = destFileName.substring(0, destFileName.lastIndexOf("/")); //windows系統(tǒng) }else{ destFileName = destDir + compressFileName.replaceAll("/", "\\\\"); destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\")); } //2創(chuàng)建文件夾 File dir = new File(destDirName); if(!dir.exists()||!dir.isDirectory()){ dir.mkdirs(); } //3解壓縮文件 fos = new FileOutputStream(new File(destFileName)); a.extractFile(fh, fos); fos.close(); fos = null; } fh = a.nextFileHeader(); } a.close(); a = null; }catch(Exception e){ throw e; }finally{ if(fos!=null){ try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();} } if(a!=null){ try{a.close();a=null;}catch(Exception e){e.printStackTrace();} } } } /** * 解壓縮 */ public static void deCompress(String sourceFile,String destDir) throws Exception{ //保證文件夾路徑最后是"/"或者"\" char lastChar = destDir.charAt(destDir.length()-1); if(lastChar!='/'&&lastChar!='\\'){ destDir += File.separator; } //根據(jù)類型,進行相應(yīng)的解壓縮 String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1); if(type.equals("zip")){ DeCompressUtil.unzip(sourceFile, destDir); }else if(type.equals("rar")){ DeCompressUtil.unrar(sourceFile, destDir); }else{ throw new Exception("只支持zip和rar格式的壓縮包!"); } } }
修改jar文件,重新打包
報錯
Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/xxxx-0.0.1-SNAPSHOT.jar'
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:261)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:247)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:109)
... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/xxxx-0.0.1-SNAPSHOT.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:287)
at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:269)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:258)
... 6 more
Socket error Event: 32 Error: 10053.
Connection closing...Socket close.
處理方案
方案一:
解壓jar文件
- jar -xvf ./*.jar
- jar -xf jar文件
修改解壓后后的文件
重新打包
- jar -cfM0 *.jar ./
方案二:
- winrar或7zip添加文件的時候, 將選擇壓縮方式由"標(biāo)準"改為存儲(Store)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

JavaMail整合Spring實現(xiàn)郵件發(fā)送功能

SpringBoot項目中連接Gauss數(shù)據(jù)庫

如何解決@value獲取不到y(tǒng)aml數(shù)組的問題

java原裝代碼完成pdf在線預(yù)覽和pdf打印及下載

Java 圖片與byte數(shù)組互相轉(zhuǎn)換實例