简体   繁体   English

如何从Jar文件中提取源代码?

[英]How do I extract source code from a Jar file?

I've received source code for a Java product to make further changes. 我收到了Java产品的源代码以进行进一步的更改。 The archive contains a bunch of JAR files. 存档包含一堆JAR文件。

Is it sufficient for developing the application, or those files for distribution only? 是否足以开发应用程序或仅用于分发的文件?

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") should contain a bunch of files with the file suffix ".java". 如果您确实收到了产品的源代码,而您所拥有的只是JAR文件,那么JAR文件(实际上是具有不同文件后缀和特定类型“清单”的ZIP文件) 应该包含一堆文件后缀为“.java”的文件。 You should be able to check this using any ZIP archive tool. 您应该能够使用任何ZIP存档工具进行检查。

If there are no ".java" files in the JARs (eg only a lot of ".class" and other files), you do not have the source code for the product. 如果JAR中没有“.java”文件(例如,只有很多“.class”和其他文件),则表示您没有该产品的源代码。 Making changes will be really really hard , given that you are not a Java developer. 鉴于您不是Java开发人员,进行更改将非常困难

Assuming that you are doing this legitimately (ie with the explicit or implicit permission of the product's developer) you will save yourself a lot of time if you can also get 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”文件。

A jar is analogous to a zip file, so they may or may not contain the source code you need for development. jar类似于zip文件,因此它们可能包含也可能不包含开发所需的源代码。 You can read about jar files here . 你可以在这里阅读jar文件。

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. 通常,您打包.class(已编译)文件并将它们分发到.jar文件中,但是可以将任何类型的文件(包括源代码)放在.jar存档中。 If you need to change the code in the library, you'll need to see the source. 如果您需要更改库中的代码,则需要查看源代码。

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. 项目中的.jar文件也可能是第三方(或内部专有)库,您根本不需要更改它们。 It's quite common to include libraries that you just use without modifying in your jar. 包含您刚刚使用的库而不在jar中进行修改是很常见的。 If the rest of your application code is accessible, you can just modify that w/o touching the library code. 如果可以访问其余的应用程序代码,则可以修改它而不用触及库代码。

Any modern Java IDE allows you to drop in jar files and use that as library code. 任何现代Java IDE都允许您放入jar文件并将其用作库代码。 This does not require source etc. You can then develop new code, that incorporates changes. 这不需要源等。然后您可以开发包含更改的代码。

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中使用ALTER代码,那么通过使用您需要更新的jar的源代码,然后使用它将会更好。

If you do not have source code, and you need to change a class, then you can decompile that class using eg JAD to a Java file (be careful, may contain bugs), and then work with that. 如果您没有源代码,并且需要更改类,那么您可以使用例如JAD将该类反编译为Java文件(小心,可能包含错误),然后使用它。 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). 你只需要在类路径中的jar前面有这个类(除非jar被密封,然后你有一个关于stackoverflow的全新问题)。

Do you have more experienced Java programmers on your team, you can ask for assistance? 您的团队中是否有更多经验丰富的Java程序员,您可以寻求帮助吗?

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. 但是,当jar文件包含一些您正在处理的项目所必需的库/包/类时,它们可以帮助您。 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. 这样,如果您使用的是Netbeans IDE,则可以将它们添加到编译路径中,并且在使用它们时甚至不会在源处窥视它们。 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 ie 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 所以,它在很大程度上取决于你如何构建你的软件,即它是增量的还是你想要修改你收到的东西...对于后者你可能会考虑去编译或要求源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM