What is the difference between JAR and packages?

Is there any difference between a JAR file and a package?

+6
java jar packages
source share
4 answers

A package is a way to logically organize your classes. For example, you can declare package com.foo; at the top of each source file that are connected enough to be in the com.foo package together. The Java compiler and runtime also expect you to put such files in the path com/foo/ , where the root of this path is the directory or JAR in your class path.

A JAR file allows you to physically organize your classes. You can take any Java files (and their parent directories, following the directory structure described above) and save them in a JAR file. A JAR file may contain files belonging to several packages, and several JAR files may contain files belonging to the same package. Thus, a JAR file is pretty much a way to store multiple class files in one physical file.

There are other features of JAR files. For example, you can specify the Main-Class value in the JAR manifest to indicate which class is the entry point for the application, and you can seal in the JAR file, which means that all classes defined in this package must be archived in one JAR file. "

+12
source share

A package is a mechanism in Java for organizing classes into namespaces. Bright is Java ARchive, a file that combines several Java classes into one.

+3
source share

In short, a JAR file is a physical file structured in the same way as a zip file that contains files used by your program at runtime. The jar will usually include .class files, executable versions of your classes, and other resources such as icons, images, language property files, etc. The Java Tutorial describes banks and how to create and manage them. (Http://java.sun.com/docs/books/tutorial/jar/index.html)

Packages are defined in this way in the Java Tutorial tutorial on interfaces and packages: "A collection of related classes and interfaces that provide security access and namespace management." (Http://java.sun.com/docs/books/tutor...va/interpack/). Although I’m sure, I don’t find that this definition in itself really gives you a good idea about which package. For more information, go to the cited page and read in this section of the tutorial.

+2
source share

In the classpath, jar and package (directory) are the same structures. Just a jar is more useful for moving between computers than a directory.

-one
source share

All Articles