如何从提取的jar文件中读取类文件?
我想读取位于.jar
包内的.class
文件。 如何从.jar
包中读取可读的.class
文件?
我的环境是:
- 语言版本:
Java
- 平台版本:
Java 1.8.0_73
- 运行时:
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
- VM服务器:
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
- 操作系统:
Windows 10 Home (64-bit) [build 10586]
编辑:
我提取的包含二进制和编译字节码的.class
文件:
我想要的输出:
使用反编译器 。 我更喜欢使用Fernflower ,或者如果你使用IntelliJ IDEA,只需打开那里的.class文件,因为它预装了Fernflower。
或者,转到javadecompilers.com ,上传.jar文件,使用CFR并下载反编译的.zip文件。
但是,在某些情况下,反编译代码是非常非法的,所以,更喜欢学习而不是反编译。
以编程方式提取.zip
/ .jar
文件的内容
假设.jar
文件是要提取的.jar
/ .zip
文件。 destDir
是它将被提取的路径:
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile); java.util.Enumeration enum = jar.entries(); while (enum.hasMoreElements()) { java.util.jar.JarEntry file = (java.util.jar.JarEntry) enum.nextElement(); java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName()); if (file.isDirectory()) { // if its a directory, create it f.mkdir(); continue; } java.io.InputStream is = jar.getInputStream(file); // get the input stream java.io.FileOutputStream fos = new java.io.FileOutputStream(f); while (is.available() > 0) { // write contents of 'is' to 'fos' fos.write(is.read()); } fos.close(); is.close(); }