Java .class文件反编译成 .java文件
1. 引入依赖
<!-- https://mvnrepository.com/artifact/org.benf/cfr --><dependency><groupId>org.benf</groupId><artifactId>cfr</artifactId><version>0.151</version></dependency>
2. 编写代码
public static void main(String[] args) throws IOException {String sourceJar = "D:\\xxx";Path sourceJarPath = Paths.get(sourceJar);String sourceJarFileName = sourceJarPath.getFileName().toString().replaceFirst("[.][^.]+$", "");File file = ResourceUtils.getFile("classpath:");String relativePath = file.getPath();String path = relativePath.substring(0, relativePath.lastIndexOf(File.separatorChar));String outputPath = path + File.separator + "cfr" + File.separator + sourceJarFileName;Long time = cfr(sourceJar, outputPath);System.out.println(String.format("decompiler time: %dms, outputPath: %s", time, outputPath));}public static Long cfr(String source, String targetPath) throws IOException {Long start = System.currentTimeMillis();// source jarList<String> files = new ArrayList<>();files.add(source);// target dirHashMap<String, String> outputMap = new HashMap<>();outputMap.put("outputdir", targetPath);OptionsImpl options = new OptionsImpl(outputMap);CfrDriver cfrDriver = new CfrDriver.Builder().withBuiltOptions(options).build();cfrDriver.analyse(files);Long end = System.currentTimeMillis();return (end - start);}