Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Mount the zip file (there are various tools), then open it using xdg-open. Remember to unmount afterwards. –  Mechanical snail Jan 10 '13 at 6:36
add comment

3 Answers 3

Using unzip you can pipe a file from the archive to stdout and then into something else to read it, like:

unzip -p /path/to/some.jar path/to/file/inside/jar | less

From memory unzip is not installed by default so you might need to:

sudo apt-get install unzip

Jeff

share|improve this answer
    
Isn't there a way of opening the file without unzipping it? –  Kurru Jan 10 '13 at 5:55
add comment

Personally I use jar because it feels more natural, and allows for other operations such as updating contents too. Here are the read commands:

List contents

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

share|improve this answer
add comment

jar files are really only zip files you can test this by doing.

file foo.jar

so to list the files you can simple do.

unzip -l foo.jar

To access the files just unzip the file with

unzip foo.jar

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.

share|improve this answer
    
Isn't there a way of opening the file without unzipping it? –  Kurru Jan 10 '13 at 5:55
    
I'll update the answer for that. –  strings Jan 10 '13 at 6:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.