java根据jar反编译指定类的源码

作者 时间
雨中星辰 2019-05-06

背景

由于领导通知需要让用户上传接口jar包,根据用户指定的接口,从jar包中反编译出源码。

一看到这个需求,第一个想到的就是大名鼎鼎的jd-gui啊。用了这么多年,一直都很好用。

开始在网上各种找,无果。
在maven仓库搜了一下,无果。
又去githup搜了一下,有了。githup地址为:https://github.com/java-decompiler/jd-gui
但是没有讲怎么用啊,囧。。。
算了,下载一下源码研究一下吧。发现用起来还是很简单的。

    /**
     *
     * @param jarPath 要反编译的jar包的绝对路径
     * @param outPath 反编译的源码输出的目录
     * @return
     * @throws DecompilerException
     * @throws IOException
     */
    public int decompile(String jarPath, String outPath) throws DecompilerException, IOException 
    /**
     * 
     * @param jarPath 要反编译的jar包的绝对路径
     * @param internalClassName 要反编译的class文件的路径:例:dcloud/common/Auth.java
     * @return
     * @throws DecompilerException
     */
    public String decompileClass(String jarPath, String internalClassName) throws DecompilerException

例:

        Decompiler decompiler = new Decompiler();
        Map decompile = decompiler.decompile("D:\\java_team\\repository\\com\\sgcc\\epri\\dcloud\\dcloud.common.Auth\\2.0.0\\dcloud.common.Auth-2.0.0.jar");
        String source = decompile.get("dcloud/common/Auth.java");
        System.out.println(source);

需要注意的是:gd-gui项目在maven仓库中没有,需要将项目中githup中下载下来,进行打包。

git clone https://github.com/java-decompiler/jd-gui.git
cd jd-gui
./gradlew build 

打包后的文件在jd-gui/build/libs中

你可能感兴趣的:(java根据jar反编译指定类的源码)