概述
- 使用Java Decompiler项目的JD-GUI从JAR文件生成源代码
- 此环境:macOS Catalina Java 11(AdoptOpenJDK 11.0.6)
什么是JD-GUI?
一个从类和JAR文件生成Java源代码的反编译器。
它还支持Java 5之后引入的注释,泛型,枚举等。
Java反编译器
JD-Core是一个从一个或多个" .class"文件重构Java源代码的库,JD-Core可用于恢复丢失的源代码并探究Java运行时库的源代码Java 5的新功能支持注释,泛型或"枚举"类型,JD-GUI和JD-Eclipse包括JD-Core库。
下载并运行
从官方网站Java Decompiler下载jd-gui-1.6.6.jar。
1 | $ wget https://github.com/java-decompiler/jd-gui/releases/download/v1.6.6/jd-gui-1.6.6.jar |
使用
java命令启动JD-GUI。
1 | $ java -jar jd-gui-1.6.6.jar |
拖放
jar文件,将其转换为Java源代码。
从
菜单中,选择File(文件)→Save All Sources(保存所有源代码)以生成包含源代码的zip文件。
尝试解压缩生成的zip文件。
1 2 3 4 5 6 7 8 9 | $ unzip helloworldmod-1.2.3.jar.src.zip Archive: helloworldmod-1.2.3.jar.src.zip creating: META-INF/ inflating: META-INF/MANIFEST.MF inflating: META-INF/mods.toml creating: com/ creating: com/example/ inflating: com/example/HelloWorldMod.java inflating: pack.mcmeta |
在生成的源代码中,jar文件所在的路径嵌入在注释中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | $ cat com/example/HelloWorldMod.java /* */ package com.example; /* */ /* */ import net.minecraft.entity.player.PlayerEntity; /* */ import net.minecraft.util.math.BlockPos; /* */ import net.minecraft.util.text.ITextComponent; /* */ import net.minecraft.util.text.StringTextComponent; /* */ import net.minecraftforge.common.MinecraftForge; /* */ import net.minecraftforge.event.entity.player.PlayerEvent; /* */ import net.minecraftforge.eventbus.api.SubscribeEvent; /* */ import net.minecraftforge.fml.common.Mod; /* */ /* */ /* */ @Mod("helloworldmod") /* */ public class HelloWorldMod /* */ { /* */ public HelloWorldMod() { /* 17 */ MinecraftForge.EVENT_BUS.register(this); /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ @SubscribeEvent /* */ public void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { /* 28 */ PlayerEntity player = event.getPlayer(); /* 29 */ BlockPos pos = player.func_180425_c(); /* */ /* */ /* */ /* 33 */ String message = "Hello, World!\n[name]=[" + player.func_200200_C_().func_150254_d() + "]\n[pos]=[" + pos.func_177958_n() + "," + pos.func_177956_o() + "," + pos.func_177952_p() + "]"; /* 34 */ StringTextComponent stringTextComponent = new StringTextComponent(message); /* 35 */ player.func_145747_a((ITextComponent)stringTextComponent); /* */ } /* */ } /* Location: /Users/johndoe/hello/build/libs/helloworldmod-1.2.3.jar!/com/example/HelloWorldMod.class * Java compiler version: 8 (52.0) * JD-Core Version: 1.1.3 */ |
参考
- Java反编译器
- Java反编译器·GitHub