22

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?

ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234

5 Answers5

24

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

This is what Intelli-J & 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 (if your version of Archive Utility on OSX allows it -- It doesn't do it for me on OSX Sierra but works on El Capitan) 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 ../

Brandon
  • 22,723
  • 11
  • 93
  • 186
4

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

  • 3
    For intellij Idea: Right click on a project-> Open module sttings->Dependencies tab->'+' button->Jars or libraries->Select your jar->Save. Then you can see jar content in project navigation window in External Libraries. – noriks Jan 21 '22 at 08:43
  • @noriks - thank you. Is there ANY way that I can edit files after importing decompiled *.jar code? – Jordan MacLachlan Feb 24 '23 at 04:26
2

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

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.

arcy
  • 12,845
  • 12
  • 58
  • 103
1

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

OHY
  • 890
  • 1
  • 8
  • 14