我想阅读位于 .class
包内的 .jar
文件。如何从.class
包中读取可读的.jar
文件?
我的环境是:
Java
Java 1.8.0_73
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
Windows 10 Home (64-bit) [build 10586]
修改
我提取的.class
文件包含二进制和&编译后的字节码:
我想要的输出:
答案 0 :(得分:9)
使用decompiler。我更喜欢使用Fernflower,或者如果您使用IntelliJ IDEA,只需从那里打开.class文件,因为它预装了Fernflower。
或者,转到javadecompilers.com,上传.jar文件,使用CFR并下载反编译的.zip文件。
但是,在某些情况下,反编译代码非常违法,因此,更愿意学习而不是反编译。
答案 1 :(得分:2)
.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();
}