Linux: what is a most convenient way to see the content of .jar file

25

6

What is a most convenient way to see the content of .jar file w/o using unzip/untar commands? What I'd like to do - is to browsing inside using cd command like it is the usual folder, seeing content, size of classes - 'ls -la'.

MC allowed to do so on the fly. Is there are any ease-in-use alternative?

javagirl

Posted 2012-04-27T12:37:43.383

Reputation: 367

@ekaj less and unzip doesnt help me - it doesn't show me the size of files in the archive for example, or date&time of last modification (unless I'll decompress it somewhere to /tmp/AAA and then will run ls -la against /tmp/AAA - which is what I want to avoid). – javagirl – 2012-05-01T17:20:38.087

Don't unzip the .zip file, just open it. You can see the sizes and modification dates. – cutrightjm – 2012-05-02T12:06:57.757

how can I open it in the linux server command line? – javagirl – 2012-05-03T15:34:58.650

@javagirl: unzip -l does show you the size of files in the archive and date & time of last modification without unpacking the package. See my answer below. Actually, this is a duplicate as ekaj suggested. – DevSolar – 2012-06-19T11:53:43.547

Answers

32

Use the jar tool that comes with the Java SDK for listing contents of a jar file. As described in http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html

$ jar tvf jarfile.jar

nshah

Posted 2012-04-27T12:37:43.383

Reputation: 421

13

Use vim to view and edit the contents of a jar file without extracting:

Open the jar in vim like this:

vim rabbitmq-client.jar

You are presented with a list of files:

1 " zip.vim version v22
2 " Browsing zipfile /var/www/sandbox/eric/rabbitmq-client.jar
3 " Select a file with cursor and press ENTER
4
5 META-INF/
6 META-INF/MANIFEST.MF
7 com/
8 com/rabbitmq/
9 com/rabbitmq/client/
10 com/rabbitmq/client/impl/
11 com/rabbitmq/client/impl/recovery/
12 com/rabbitmq/tools/
13 com/rabbitmq/tools/json/
14 com/rabbitmq/tools/jsonrpc/
15 com/rabbitmq/utility/

Put the cursor over the META-INF/MANIFEST.MF and press Enter. You see this:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_31-b31 (Sun Microsystems Inc.)
Export-Package: com.rabbitmq.client;version="3.3.5";uses:="com.rabbitm
 q.client.impl,com.rabbitmq.utility,javax.net,javax.net.ssl,javax.secu
 rity.auth.callback,javax.security.sasl",com.rabbitmq.client.impl;vers
 ion="3.3.5";uses:="com.rabbitmq.client,com.rabbitmq.utility,javax.net
 ",com.rabbitmq.client.impl.recovery;version="3.3.5";uses:="com.rabbit
 mq.client,com.rabbitmq.client.impl",com.rabbitmq.tools;version="3.3.5
 ";uses:="com.rabbitmq.utility",com.rabbitmq.tools.json;version="3.3.5
 ",com.rabbitmq.tools.jsonrpc;version="3.3.5";uses:="com.rabbitmq.clie
 nt",com.rabbitmq.utility;version="3.3.5"
Bundle-Vendor: SpringSource
Bundle-Version: 3.3.5
Tool: Bundlor 1.0.0.RELEASE
Bundle-Name: RabbitMQ Java AMQP client library
Bundle-ManifestVersion: 2
Bundle-SymbolicName: com.rabbitmq.client
Import-Package: javax.net;version="0",javax.net.ssl;version="0",javax.
 security.auth.callback;version="0",javax.security.sasl;version="0"

Name: rabbitmq-client
Specification-Title: AMQP
Specification-Version: 0.9.1
Specification-Vendor: AMQP Working Group (www.amqp.org)
Implementation-Title: RabbitMQ
Implementation-Version: 3.3.5
Implementation-Vendor: Rabbit Technologies Ltd. (www.rabbitmq.com)

Change a few lines using normal vim editing commands.

press 'i' to enter insert mode
edit your lines
press :wq<enter> to write and quit

You are taken back to a list of files in the jar, quit out.

:q <enter>

Check to see if it the changes are permanent:

Follow the above steps again to inspect the file again, 
the change should still be there.

Eric Leschinski

Posted 2012-04-27T12:37:43.383

Reputation: 5 303

Great staff, thanks. Although right answer (as for me) is unzip - but that vim's example really appreciated. – setevoy – 2016-01-15T20:02:33.240

why my jar file content showing binary file ? – John Joe – 2017-02-13T01:53:26.597

@JohnJoe: Because your Vim does not identify it as a ZIP archive (which a .jar effectively is). This hints at a broken, or too-basic Vim setup. If that problem persists, consider taking it to vi.SE.

– DevSolar – 2017-11-23T08:20:29.420

1@JohnJoe, you have to install the latest vim: yum install --assumeyes vim-enhanced – TraxX – 2019-07-16T02:31:32.647

8

This one was always enough for me:

unzip -l <jarfile>

DevSolar

Posted 2012-04-27T12:37:43.383

Reputation: 3 860

2

I think .jar files are basically .zip files. So, I guess, fuse-zip could work.

Der Hochstapler

Posted 2012-04-27T12:37:43.383

Reputation: 77 228

1This is the answer I would go with if you actually want to navigate to the .zip archive like a folder. (or at least that is how I understood that it works) – cutrightjm – 2012-04-27T20:00:46.133

I can't install additional stuff to server, unless I wouldn't ask this question just by installing MC – javagirl – 2012-04-30T09:29:20.740

1

  • To list the content: jar tvf jarfile.jar
  • To extract: jar xvf jarfile.jar

Athi

Posted 2012-04-27T12:37:43.383

Reputation: 11

This seems to be potentially a good answer but you should try to make it "beefier". Answer(s) that can fit into one or two lines are often typically better off as comments (once you have enough rep). – Anaksunaman – 2018-11-21T12:38:08.767

1

Just rename the .jar file to a .zip. IE, test.jar to test.zip. You will be able to see all of the compiled classes. If you want to view the source its self, however, you will need a decompiler.

cutrightjm

Posted 2012-04-27T12:37:43.383

Reputation: 3 966

sorry, i didn't get it. what is the difference? i still cannot do ls, cd command against *.zip file. – javagirl – 2012-05-01T17:16:09.340

@javagirl You can only open .zip files, as far as I know, by clicking on them if you are not going to install any additional software. IE, you aren't going to cd to them because they are not directories. – cutrightjm – 2012-05-02T12:06:03.093

if you'd use 'mc' in linux you would knew that you can browse over content of zip/jar file, because it makes unzipping on fly. My question was about the alternative to 'mc', because I heard a lot of linux guru saying they can avoid 'mc' and use pure command line with the same result and efficiency. And I cannot 'clicking' on zip file as far as I'm on the linux server command line – javagirl – 2012-05-03T15:34:25.423

Well, this is an alternative. – cutrightjm – 2012-05-03T16:05:50.233

1

In case you wish to see the contents in date order as I did:

jar tvf jarfile.{e,j,w}ar | sort -k 7,7 -k 3,3M -k 4,4n -k5,5

JStrahl

Posted 2012-04-27T12:37:43.383

Reputation: 141

0

You could use an ssh mount to have server's file system locally. After it you can use any locally available software to navigate the file system, e.g. Far Manager is very powerful to navigate into archives.

In this solution you don't need any additional software on the server, only sshd.

kan

Posted 2012-04-27T12:37:43.383

Reputation: 131