---
title: "记一次解决maven项目无法编译的问题"
author: "hellohank"
date: "2022-05-17T17:17:31Z"
tags: []
categories: ["开源软件"]
slug: "maven"
draft: false
lastmod: "2022-05-17T17:17:45Z"
id: 2532149
url: "https://hellohank.iteye.com/blog/2532149"
---

在idea中编译时，出现以下错误

```
Malformed \uxxxx encoding.
```

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

找到并删除这些有问题的jar包的方法，我写了一个java类来操作（依赖commons-io包），具体如下：

 

```
package com.tmp;

import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class FindInvalidJarAndDel extends DirectoryWalker<String> {
    public static void main(String[] args) throws IOException {
        // 查找本地maven仓库
        File startDir = new File("C:\\Users\\beta\\.m2\\repository");
        FindInvalidJarAndDel finder = new FindInvalidJarAndDel();
        List<String> finded = new ArrayList<>();
        finder.walk(startDir, finded);
        if (finded.size() > 0) {
            //删除对应的文件
            for (String f : finded) {
                System.out.println(f);
                try {
                    FileUtils.forceDelete(new File(f));//删除整个文件夹
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected void handleFile(File file, int depth, Collection<String> results) throws IOException {
        if (results.contains(file.getParent())) {
            return;
        }
        if (file.getName().endsWith(".lastUpdated")||file.getName().toLowerCase().endsWith("resolver-status.properties")) {
            results.add(file.getParent());
            return;
        }
        if (file.getName().endsWith(".jar")) {
            //尝试解压一下，如果不能解压，则说明jar包有问题
            try {
                ZipFile zip = new ZipFile(file);
                Enumeration zipEntries = zip.entries();
                while (zipEntries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) zipEntries.nextElement();
                    entry.getName();
                    entry.getSize();
                }
            } catch (Exception e) {
                results.add(file.getParent());
                return;
            }
        }
    }
}
```