解决 maven 项目无法编译的问题

580阅读 0评论2022-05-19 fhadmin
分类:Java


点击(此处)折叠或打开


  1. 在 idea 中编译时,出现以下错误

  2. 代码

  3. Malformed \uxxxx encoding.  
  4.  经过各种资料和分析后,是因为依赖的 jar 包有下载不完整导致的。解决办法是,把这些本地 maven 仓库中下载有问题的 jar 包删除,重新打包项目即可(idea 中就是 reload)。

  5. 找到并删除这些有问题的 jar 包的方法,我写了一个 java 类来操作(依赖 commons-io 包),具体如下:

  6. package com.tmp;
  7.   
  8. import org.apache.commons.io.DirectoryWalker;
  9. import org.apache.commons.io.FileUtils;
  10.   
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.Enumeration;
  16. import java.util.List;
  17. import java.util.zip.ZipEntry;
  18. import java.util.zip.ZipFile;


  19. /**
  20.  * 说明:验证jar完整
  21.  * 作者:FH Admin
  22.  * from:fhadmin.cn
  23.  */
  24. public class FindInvalidJarAndDel extends DirectoryWalker<String> {
  25.     public static void main(String[] args) throws IOException {
  26.         // 查找本地maven仓库
  27.         File startDir = new File("C:\\Users\\beta\\.m2\\repository");
  28.         FindInvalidJarAndDel finder = new FindInvalidJarAndDel();
  29.         List<String> finded = new ArrayList<>();
  30.         finder.walk(startDir, finded);
  31.         if (finded.size() > 0) {
  32.             //删除对应的文件
  33.             for (String f : finded) {
  34.                 System.out.println(f);
  35.                 try {
  36.                     FileUtils.forceDelete(new File(f));//删除整个文件夹
  37.                 } catch (IOException e) {
  38.                     e.printStackTrace();
  39.                 }
  40.             }
  41.         }
  42.     }
  43.   
  44.     protected void handleFile(File file, int depth, Collection<String> results) throws IOException {
  45.         if (results.contains(file.getParent())) {
  46.             return;
  47.         }
  48.         if (file.getName().endsWith(".lastUpdated")||file.getName().toLowerCase().endsWith("resolver-status.properties")) {
  49.             results.add(file.getParent());
  50.             return;
  51.         }
  52.         if (file.getName().endsWith(".jar")) {
  53.             //尝试解压一下,如果不能解压,则说明jar包有问题
  54.             try {
  55.                 ZipFile zip = new ZipFile(file);
  56.                 Enumeration zipEntries = zip.entries();
  57.                 while (zipEntries.hasMoreElements()) {
  58.                     ZipEntry entry = (ZipEntry) zipEntries.nextElement();
  59.                     entry.getName();
  60.                     entry.getSize();
  61.                 }
  62.             } catch (Exception e) {
  63.                 results.add(file.getParent());
  64.                 return;
  65.             }
  66.         }
  67.     }
  68. }



上一篇:java 定时任务 quartz 时间表达式Cron总结
下一篇:Spring Boot 2.7.0 更新说明