ShanZhengYang
ShanZhengYang

Reputation: 17651

How to extract the source code from a *.jar file on a Mac?

I'm very confused. I downloaded a *.jar file as a bit of software. So, I would like to extract the source code to look at it

I used the command jar xf filename.jar

which returned two more *.jar files and a *.class file. I still cannot open these in the terminal with standard text editors.

Perhaps this is not open source software? Is there an alternative to see what has been done here?

Upvotes: 23

Views: 87681

Answers (5)

Brandon
Brandon

Reputation: 23485

Run "java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar"

This is what IntelliJ & Android-Studio Decompiler does.

Note:

Fernflower extracts the .java files to a .jar file.

You can either Unzip the jar file as a regular zip file OR you can do jar xf DecompiledJar and it'll extract it.

Example (all in one command -- multiple commands separated by &&):

java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar && cd DecompiledJar && jar xf DecompiledJar.jar && cd ../

Upvotes: 29

V Surya Kumar
V Surya Kumar

Reputation: 83

Adding to the solution provided by @saravanan.

Another solution is to use Eclipse IDE to extract the files.

  1. Open the Eclipse
  2. Paste the jar file in any of the projects.
  3. Right-click on the jar file under Package Explorer.
  4. Extract the file to the desired location.

Upvotes: 2

OHY
OHY

Reputation: 970

Another solution is to use JD for OSX (http://java-decompiler.github.io/). For Java 11 with BigSur version, make sure to fix a common error by adding the line export JAVA_HOME=$(/usr/libexec/java_home -v11) into the file /Applications/JD-GUI.app/Contents/MacOS/universalJavaApplicationStub.sh as suggested in https://github.com/java-decompiler/jd-gui/issues/332

Upvotes: 1

saravanan saminathan
saravanan saminathan

Reputation: 702

Easy solution:

If you have eclipse just add the jar file in the classpath of current project u can see all the packages and source code in the jar. You no need to install and use the commands. You will get a better view of all files

Upvotes: 4

arcy
arcy

Reputation: 13143

A jar file may contain source code, but more commonly contains only class files. Class files are normally for execution, not for extracting source.

You can decompile class files into source code, but the decompiled code will not be nearly as helpful as the original source code.

If it is open source, go back to the site where you downloaded the jar files and look for the source. It might be in Zip files to be downloaded, it might be in jar files to be downloaded, and it might be in some kind of repository, like Git, that you can connect to with the right software.

Upvotes: 2

Related Questions