How can I open and see within a file within a jar file from within Terminal?
I can use less to print out all the files within the jar file, but how can I open these files?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
Personally I use
jar because it feels more natural, and allows for other operations such as updating contents too. Here are the read commands:
jar tf myjar.jar
Extract contents to pwd
jar xf myjar.jar
Extract specific file, creating dirs as required
jar xf myjar.jar /foo/bar.class
Extracting the bytecode itself isn't usually terribly useful though. To make sense of it, you need to decompile it into something readable.
There aren't many good java decompilers to choose from. I tend to use JD-GUI, because it's quick and does what I need it to. Frustratingly, its multi-file search functionality never works for me (note that Ctrl+F search-on-the-page is fine). This is no great loss though; you can get it to dump all the sources to disk with Ctrl+Alt+S for perusal with grep/vim/Eclipse etc.
I install it like this:
wget http://jd.benow.ca/jd-gui/downloads/jd-gui-0.3.5.linux.i686.tar.gz tar xzf jd-gui-0.3.5.linux.i686.tar.gz sudo mv jd-gui /usr/local/bin/ sudo chown root:root /usr/local/bin/jd-gui sudo chmod 755 /usr/local/bin/jd-gui
Using it goes something like this:
jd-gui myjar.jar Ctrl+Alt+S; pick your path & save Alt+F4 jar xf <path>/myjar.src.zip
Note however that JD-GUI has no CLI interface. If you know of a good CLI decompiler, I'd be very interested to hear of it (there is JAD, but AFAIK that only supports Java < 1.5).
jar files are really only zip files you can test this by doing.
so to list the files you can simple do.
unzip -l foo.jar
To access the files just unzip the file with
If you want to extract only one file then append the files after
unzip foo.jar some/path/in/jar/bar.class
Make sure to use the full path from -l. And keep in mind it will create the same path locally.
(No rep points, so have to answer to respond to comments by @Kurro)
unzip (-p | -c) <jar-file> <file-in-jar>
-c options extract the file to standard output;
-c gives an extra 2 lines of data at the beginning (which JAR file and which file within the archive).
I found this extremely convenient for reading the Manifest file or the .xml files. For the compiled
.class files, this is useless, but your friend here is
javap -classpath <jar-file> <fully.qualified.class.name> # without the .class suffix
Some useful options are:
-c (decompile to Java Assembly),
-private to also show private fields, and
-verbose for lots of extra information. If it's a big file, you may want to pipe the output through