Jar 文件到底包含什么?

作为一名实习生,我在项目中使用公司代码,他们通常会发给我一个 jar文件。我把它添加到构建 path中的 Eclipse,通常一切都很好。

但是,我很好奇,每个类包含什么,当我试图打开 jar文件中的一个类时,它告诉我需要一个源文件。

这是什么意思?我来自一个 C/C + + 的背景,所以是一个 jar类似于一个已经编译的 .o文件,所有我可以看到的是 .h的东西?还是我用的 jar文件中真的有加密的代码,所以我无法读取?

谢谢你的回答!

编辑: < br/> 谢谢,伙计们,我知道这有点像一个存档,但我很困惑为什么当我试图打开 .class文件时,我得到了一堆随机的字符。当我尝试用 C 打开一个 .o文件时,输出是相似的,所以我只是想确认一下。
谢谢!

130007 次浏览

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

Go to command line and execute jar tvf jarfilename.jar

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

However, Java can be decompiled - in case the developer obfuscated his code you won't get any useful class/function/variable names though.

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

A jar file is basically a zip file containing .class files and potentially other resources (and metadata about the jar itself). It's hard to compare C to Java really, as Java byte code maintains a lot more metadata than most binary formats - but the class file is compiled code instead of source code.

If you either open the jar file with a zip utility or run jar xf foo.jar you can extract the files from it, and have a look at them. Note that you don't need a jar file to run Java code - classloaders can load class data directly from the file system, or from URLs, as well as from jar files.

A .jar file is akin to a .exe file. In essence, they are both executable files.

A jar file is also a archive (JAR = Java ARchive). In a jar file, you will see folders and class files. Each .class file is similar to a .o you might get from C or C++, and is a compiled java archive.

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

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 are also able to deal with .jar files.)

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

Jar files can contain any kind of files, but they usually contain class files and supporting configuration files (properties), graphics and other data files needed by the application.

Class files contain compiled Java code, which is executable by the Java Virtual Machine.

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

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

JD-GUI is a very handy tool for browsing and decompiling JARs

A .jar file contains compiled code (*.class files) and other data/resources related to that code. It enables you to bundle multiple files into a single archive file. It also contains metadata. Since it is a zip file it is capable of compressing the data that you put into it.

Couple of things i found useful.

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

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

The book OSGi in practice defines JAR files as, "JARs are archive files based on the ZIP file format, allowing many files to be aggregated into a single file. Typically the files contained in the archive are a mixture of compiled Java class files and resource files such as images and documents. Additionally the specification defines a standard location within a JAR archive for metadata — the META-INF folder — and several standard file names and formats within that directly, most important of which is the MANIFEST.MF file."

JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The JAR format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a JAR file may be digitally signed by the applet author to authenticate their origin.

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

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

1.To create 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 Jar File (Unzip File)

    jar -xvf NameOfJarFile.jar

3.To display table of content

    jar -tvf NameOfJarFile.jar

While learning about JAR, I came across this thread, but couldn't get enough information for people like me, who have .NET background, so I'm gonna add few points which can help persons like myself with .NET background.

First we need to define similar concept to JAR in .NET which is Assembly and assembly shares a lot in common with Java JAR files.

So, an assembly is the fundamental unit of code packaging in the .NET environment. Assemblies are self contained and typically contain the intermediate code from compiling classes, metadata about the classes, and any other files needed by the packaged code to perform its task. Since assemblies are the fundamental unit of code packaging, several actions related to interacting with types must be done at the assembly level. For instance, granting of security permissions, code deployment, and versioning are done at the assembly level.

Java JAR files perform a similar task in Java with most differences being in the implementation. Assemblies are usually stored as EXEs or DLLs while JAR files are stored in the ZIP file format.

Source of Information -> 5- Assemblies