What exactly does the jar file contain?

As an intern, I use company code in my projects, and they usually send me a jar file for work. I add it to the path assembly in Eclipse and usually everything is fine and dandy.

However, I was curious to know what each class contained, and when I try to open one of the classes in the jar file, it tells me that I need the source file.

What does it mean? I come from the background of C / C ++, so that the jar looks like an already compiled .o file, and all I see is the stuff .h ? Or is there actual code in the jar file that I use for encryption, so I can't read it?

Thanks for all the answers!

Edit:
Thanks guys, I knew this looked like an archive, but I was confused by the fact that when I tried to open .class files, I got a bunch of random characters. The result was similar when I tried to open the .o file in C, so I just wanted to make sure.
Thank!

+44
java jar
Aug 22 '12 at 18:29
source share
12 answers

The JAR file is actually just a ZIP file. It can contain anything - usually it contains compiled Java code (* .class), but sometimes also Java source code (* .java).

However, Java can be decompiled - in case the developer has confused his code, you will not get any useful class / function / variable names.

+49
Aug 22 '12 at 18:31
source share

However, I was curious what each class contained, and when I try to open one of the classes in the jar file, it tells me that I need the source file.

A jar file is basically a zip file containing .class files and potentially other resources (and metadata about the bank itself). It is difficult to compare C with Java in fact, since Java byte code supports much more metadata than most binary formats, but the class file is compiled instead of the source code.

If you open the jar file with a zip utility or run jar xf foo.jar , you can extract the files from it and look at them. Please note that you do not need a jar file to run Java code. Class loaders can load class data directly from the file system or from URLs, as well as from jar files.

+41
Aug 22 '12 at 18:31
source share

The best way to understand what the jar file contains is by doing this:

Go to command prompt and execute jar tvf jarfilename.jar

+14
Aug 22 '12 at 18:30
source share

The A jar file is a zip file with some additional files containing metadata. (Despite the .jar extension, it is in zip format, and any utilities that deal with .zip files can also process .jar files.)

http://docs.oracle.com/javase/8/docs/technotes/guides/jar/index.html

Jar files can contain any files, but usually they contain class files and supporting files (properties), image files, and other data files necessary for the application.

Class files contain compiled Java code executed by the Java virtual machine.

http://en.wikipedia.org/wiki/Java_class_file

+9
Aug 22 '12 at 18:33
source share

JAR stands for Java Archive. This is a file format based on the popular ZIP file format and is used to combine many files into one. Although the JAR can be used as a general tool for archiving, the main motivation for its development was that Java applets and their necessary components (files, images and sounds) can be downloaded to the browser in only one HTTP transaction, and not in opening a new one connection for every detail. This greatly improves the speed at which an applet can be loaded onto a web page and get started. The JAR format also supports compression, which reduces file size and increases download time even further. In addition, individual entries in the JAR file can be digitally signed by the applet author to authenticate their origin.

+5
Jul 11 '14 at 6:05
source share

The jar file contains compiled Java binary classes in the form of *.class , which can be converted to a readable .java class by decompiling it using some open source decompiler. The bank also has an optional META-INF/MANIFEST.MF that tells us how to use the jar file - indicates other jar files to be downloaded using jar.

+3
Aug 22 2018-12-18T00:
source share

JD-GUI is a very convenient tool for viewing and decompiling JAR

+2
Aug 22 '12 at 18:52
source share

The .jar file is akin to the .exe file. In fact, they are both executable zip files (different zip algorithms).

In the jar file you will see folders and class files. Each class file is similar to your .o file and is a compiled java archive.

If you want to see the code in the jar file, download the java decompiler (located here: http://java.decompiler.free.fr/?q=jdgui ) and the .jar extractor (7zip works fine).

+1
Aug 22 '12 at 18:32
source share

Just check if aopalliance.jar a .java file instead of .class files. if so, just extract the jar file, import it into eclipse and create a jar though eclipse. It worked for me.

+1
Mar 08 '16 at 7:41
source share

The .jar file contains compiled code (* .class files) and other data / resources associated with this code. This allows you to merge several files into one archive file. It also contains metadata. Since this is a zip file, it is capable of compressing the data that you insert into it.

A few things I found useful.

http://www.skylit.com/javamethods/faqs/createjar.html

http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html

The OSGi book in practice defines JAR files as "JAR files are ZIP-based archive files that allow you to combine multiple files into a single file. Typically, files contained in an archive are a mixture of compiled Java class files and resource files, such as images and documents. In addition, the specification defines the standard location in the JAR archive for metadata - the META-INF folder - and several standard file names and formats inside this important of which is the MANIFEST.MF file. "

0
Jul 19 '13 at 5:05
source share

According to my understanding, a Jar file is similar to Zip, but it has executable files.

If you do not have executables in the Jar, then it will be the same as the Zip file, I want to say why go to the Jar.

Since the jar file has executable files, they can be easily downloaded to your code and easily transferred to others to use it.

0
Sep 04 '15 at 5:01
source share

Jar (Java Archive) contains a group of .class files.

1.To create a Jar file (Zip File)

  if one .class (say, Demo.class) then use command jar -cvf NameOfJarFile.jar Demo.class (usually it's not feasible for only one .class file) if more than one .class (say, Demo.class , DemoOne.class) then use command jar -cvf NameOfJarFile.jar Demo.class DemoOne.class if all .class is to be group (say, Demo.class , DemoOne.class etc) then use command jar -cvf NameOfJarFile.jar *.class 

2.To extract the jar file (unzip the file)

  jar -xvf NameOfJarFile.jar 

3. To display a table of contents

  jar -tvf NameOfJarFile.jar 
0
Jun 06 '16 at 8:22
source share



All Articles