简体   繁体   中英

How do I extract source code from a Jar file?

I've received source code for a Java product to make further changes. The archive contains a bunch of JAR files.

Is it sufficient for developing the application, or those files for distribution only?

I've received source code for a Java product ...

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". You should be able to check this using any ZIP archive tool.

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. Making changes will be really really hard , given that you are not a Java developer.

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).

A jar is analogous to a zip file, so they may or may not contain the source code you need for development. You can read about jar files here .

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.

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.

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 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.

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. 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).

Do you have more experienced Java programmers on your team, you can ask for assistance?

you can try decompiling the jar files back into their source, otherwise the jar files won't help much for development.

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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