解決jar包rar壓縮后無法運(yùn)行問題
jar包rar壓縮后無法運(yùn)行
報(bào)錯(cuò)為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)該問題,請(qǐng)檢查jar包壓縮時(shí)是否用壓縮軟件方式打開。
如果丟進(jìn)去的是第三方j(luò)ar包或者更新后的引入lib包,復(fù)制到壓縮目錄時(shí)需要選擇壓縮方式,一定要選擇存儲(chǔ)方式。
不然rar默認(rèn)會(huì)給你再次壓縮,部署到linux后項(xiàng)目就無法正常運(yùn)行。
再次壓縮后大小與未壓縮時(shí)不一致,所以會(huì)報(bào)錯(cuò)。
一定要選擇存儲(chǔ)方式復(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格式壓縮包 * 對(duì)應(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工具默認(rèn)壓縮編碼為UTF-8編碼, 而winRAR軟件壓縮是用的windows默認(rèn)的GBK或者GB2312編碼 所以解壓縮時(shí)要制定編碼格式 */ e.setEncoding("gbk"); e.execute(); }catch(Exception e){ throw e; } } /** * 解壓rar格式壓縮包。 * 對(duì)應(yīng)的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又會(huì)用到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ù)類型,進(jìn)行相應(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文件,重新打包
報(bào)錯(cuò)
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添加文件的時(shí)候, 將選擇壓縮方式由"標(biāo)準(zhǔn)"改為存儲(chǔ)(Store)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaMail整合Spring實(shí)現(xiàn)郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了JavaMail整合Spring實(shí)現(xiàn)郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08SpringBoot項(xiàng)目中連接Gauss數(shù)據(jù)庫
本文主要介紹了SpringBoot項(xiàng)目中連接Gauss數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06如何解決@value獲取不到y(tǒng)aml數(shù)組的問題
文章介紹了在使用YAML配置文件時(shí),通過@Value注解獲取整數(shù)和數(shù)組列表的配置方法,并提供了兩種解決方案:一種適用于非嵌套列表,另一種適用于嵌套列表等復(fù)雜配置2024-11-11基于Java在netty中實(shí)現(xiàn)線程和CPU綁定
這篇文章主要介紹了基于Java在netty中實(shí)現(xiàn)線程和CPU綁定,文章圍繞主題的相關(guān)內(nèi)容展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05java原裝代碼完成pdf在線預(yù)覽和pdf打印及下載
本文主要介紹了java原裝代碼完成pdf在線預(yù)覽和pdf打印及下載的方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02Java 圖片與byte數(shù)組互相轉(zhuǎn)換實(shí)例
下面小編就為大家?guī)硪黄狫ava 圖片與byte數(shù)組互相轉(zhuǎn)換實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02