Editing a .jar with `vim`

7

1

On an Amazon Linux instance I'm trying to edit a jar file remotely through putty. vim provides a convenient navigator, which displays a prompt with a list of the contained files in the jar:

> vim filename.jar

" zip.vim version v22
" Browsing zipfile /home/ec2-user/tigase/libs/tigase-muc.jar
" Select a file with cursor and press ENTER

      453  01-14-2013 10:01   META-INF/MANIFEST.MF
      110  01-14-2013 10:01   META-INF/maven/tigase/tigase-muc/pom.properties
     4675  01-14-2013 10:01   META-INF/maven/tigase/tigase-muc/pom.xml
     5751  01-14-2013 10:01   tigase/component/AbstractComponent.class
     2337  01-14-2013 10:01   tigase/component/AbstractComponent$1.class
     ....

However after selecting a file (eg. AbstractComponent.class) and press Enter, within vim I get:

caution: filename not matched:       5751  01-14-2013 10:01   tigase/component/AbstractComponent.class

and the file doesn't open.

As a side note, I also noticed that if I extract the jar (either with unzip or jar) and open an extracted file with vim, the contents are misformatted:

Êþº¾^@^@^@2^A^[^H^@     ^H^@
^H^@^K^H^@^U^H^@^V^H^@!^H^@#^H^@^^A^@) stanza already with type='...
^A^@^C()I^A^@^C()V^A^@^C()Z^A^@^F<init>^A^@^NDEFAULT_WRITER^A^@^WFE...
....

Any ideas how to overcome the error or formatting issue?

ile

Posted 2014-02-05T09:43:07.803

Reputation: 183

2You are aware that jar is basically a zip, and that .class files are Java bytecode files? Meaning they're not text files, but inly data containers and you'd need a decompiled (or a hex editor and a steady eye) to read them. – Bobby – 2014-02-05T10:04:16.433

Answers

4

However after selecting a file (eg. AbstractComponent.class) and press Enter, within vim I get:

caution: filename not matched

This is probably a known bug in vim's ZIP plugin - the plugin does not properly handle ZIP files that contain ZIP comments. See e.g. this mailing list post

As a side note, I also noticed that if I extract the jar (either with unzip or jar) and open an extracted file with vim, the contents are misformatted

This is because most files inside a JAR are compiled Java class files (file suffix .class). These are binary data, and vim is not really suitable for editing them, because vim is a text editor, not a binary editor.

You can edit them in vim using the xxd command (see "Using xxd" in the vim docs), or you can use a hex editor, such as bvi.

At any rate, directly viewing the contents of a class file is rarely helpful, as you need to understand the binary class file format to read them.

Could you explain why you are trying to open files inside a JAR? Then maybe we can help.

sleske

Posted 2014-02-05T09:43:07.803

Reputation: 19 887

'grats on 10k!~ – Sathyajith Bhat – 2014-02-05T12:17:07.270

I had apparently spaced out thinking of .java as of .class (compiled binaries) and expecting to be able to edit those files directly, i.e. not needing to extract - edit - compress. I will look up and try 'xxd', thanks a bunch! – ile – 2014-02-05T12:23:02.410

@Sathya: Thanks, I just got the message :-). – sleske – 2014-02-05T14:41:32.193

1

This is how I solved the formatting issues.

First install emacs

 yum install emacs

Then use below command to read the content

emacs somejar.jar

select the content and press Enter

To save and quit from the editor, press C-x C-s (Ctrl+x, followed by Ctrl+s).

Source : How To Use the Emacs Editor in Linux

John Joe

Posted 2014-02-05T09:43:07.803

Reputation: 143

3The question is about how do this job with vim. – artificerpi – 2017-07-11T07:17:50.827

This answer is not on topic, doesn't help the community, and follows the pattern: have one (small?) problem with X — plz switch and start using Y. – kgadek – 2018-12-31T11:57:54.280