java 如何从 Jar 文件中提取源代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3171758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract source code from a Jar file?
提问by SharpAffair
I've received source code for a Java product to make further changes. The archive contains a bunch of JAR files.
我收到了 Java 产品的源代码以进行进一步更改。存档包含一堆 JAR 文件。
Is it sufficient for developing the application, or those files for distribution only?
是否足以开发应用程序或仅用于分发的文件?
回答by Stephen C
I've received source code for a Java product ...
我收到了一个 Java 产品的源代码......
If you have really received the source code of a product, and all you've got is JAR files, then the JAR files (which are actually ZIP files with a different file suffix and a particular kind of "manifest") shouldcontain a bunch of files with the file suffix ".java". You should be able to check this using any ZIP archive tool.
如果您真的收到了产品的源代码,并且您所拥有的只是 JAR 文件,那么 JAR 文件(实际上是具有不同文件后缀和特定类型“清单”的 ZIP 文件)应该包含一堆文件后缀为“.java”的文件。您应该可以使用任何 ZIP 存档工具进行检查。
If there are no ".java" files in the JARs (e.g. only a lot of ".class" and other files), you do not have the source code for the product. Making changes will be really really hard, given that you are not a Java developer.
如果 JAR 中没有“.java”文件(例如只有很多“.class”和其他文件),则您没有产品的源代码。考虑到您不是 Java 开发人员,进行更改将非常困难。
Assuming that you are doing this legitimately (i.e. with the explicit or implicit permission of the product's developer) you will save yourself a lot of time if you can alsoget hold of the product's build instructions. For example, if it is built using Ant, you want the "build.xml" file(s).
假设您这样做是合法的(即在产品开发人员的明确或隐含许可下),如果您还可以获得产品的构建说明,您将节省大量时间。例如,如果它是使用 Ant 构建的,您需要“build.xml”文件。
回答by SharpAffair
回答by Bill the Lizard
Normally you package up .class (compiled) files and distribute them in .jar files, but it is possible to put any kind of file you want (including source code) in a .jar archive. If you need to change the code in the library, you'll need to see the source.
通常,您打包 .class(已编译)文件并将它们分发到 .jar 文件中,但也可以将您想要的任何类型的文件(包括源代码)放入 .jar 存档中。如果您需要更改库中的代码,则需要查看源代码。
It's also possible that the .jar files in your project are 3rd-party (or internal proprietary) libraries that you won't need to change at all. It's quite common to include libraries that you just use without modifying in your jar. If the rest of your application code is accessible, you can just modify that w/o touching the library code.
您项目中的 .jar 文件也可能是您根本不需要更改的第 3 方(或内部专有)库。在 jar 中包含您只是使用而无需修改的库是很常见的。如果您的应用程序代码的其余部分可以访问,您只需修改它,而无需触及库代码。
回答by Thorbj?rn Ravn Andersen
Any modern Java IDE allows you to drop in jar files and use that as library code. This does not require source etc. You can then develop newcode, that incorporates changes.
任何现代 Java IDE 都允许您放入 jar 文件并将其用作库代码。这不需要源代码等。然后您可以开发包含更改的新代码。
If you need to ALTER code inside the jars, you would be much, much better of by having the source code for that jar you need to update, and then work with that.
如果您需要更改 jar 中的代码,那么通过拥有需要更新的那个 jar 的源代码,然后使用它,您会好得多。
If you do not have source code, and you need to change a class, then you can decompile that class using e.g. JAD to a Java file (be careful, may contain bugs), and then work with that. You just need to have that class in front of the jar in the classpath (unless the jar is sealed, and then you have a whole new question for stackoverflow).
如果您没有源代码,并且您需要更改一个类,那么您可以使用例如 JAD 将该类反编译为 Java 文件(小心,可能包含错误),然后使用它。你只需要在类路径中的 jar 前面有那个类(除非 jar 是密封的,然后你有一个关于 stackoverflow 的全新问题)。
Do you have more experienced Java programmers on your team, you can ask for assistance?
您的团队中是否有更有经验的 Java 程序员,您可以寻求帮助吗?
回答by jgt
you can try decompiling the jar files back into their source, otherwise the jar files won't help much for development.
您可以尝试将 jar 文件反编译回其源代码,否则 jar 文件对开发没有多大帮助。
jar files can however help you when they contain some libraries/packages/classes that you find essential for a project you are working on. That way you can add them to your compilation path if you are using Netbeans IDE and use them without even peeking at the source. However this will work best if you are doing some kind of incremental development on your project. so, it highly depends on how you are building your software i.e. is it incremental or might you want to modify what you received... for the latter you ,might consider de-compilation or asking for the source code
然而,当 jar 文件包含一些您认为对您正在处理的项目必不可少的库/包/类时,它们可以为您提供帮助。这样,如果您使用 Netbeans IDE,就可以将它们添加到编译路径中,甚至无需查看源代码即可使用它们。但是,如果您正在对项目进行某种增量开发,这将最有效。所以,这在很大程度上取决于你如何构建你的软件,即它是增量的还是你想修改你收到的……对于后者,你可能会考虑反编译或索取源代码

