Is there any difference between a JAR file and a package?
In classpath jar and package (directory) is same structures. Just jar more usefull for moving between computers than directory.
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 related enough to reside in the com.foo
package together. The Java compiler and runtime will also expect you to place such files in the path com/foo/
, where the root of this path is a directory or JAR in your classpath.
A JAR file lets you physically organize your classes. You can take any Java files (and their parent directories, respecting the directory structure discussed above) and store them in a JAR file. A JAR file may contain files belonging to multiple packages, and multiple JAR files may contain files that belong to the same package. So, a JAR file is largely a way to store multiple class files in a single physical file.
There are some other special characteristics of JAR files. For example, you can specify a Main-Class value in the JAR manifest to designate which class is the entry point for an application, and you can seal packages in a JAR file, "which means that all classes defined in that package must be archived in the same JAR file."
A package is a mechanism in Java for organizing classes into namespaces. A jar is a Java ARchive, a file that aggregates multiple Java classes into one.
In brief, a JAR file is a physical file, structured much like a zip file, that contains files used by your program at execution time. A jar will typically include .class files, the executable versions of your classes, and other resources like icons, pictures, language-specific properties files, etc. The Java Tutorial describes jars, and how to create/manage them. (http://java.sun.com/docs/books/tutorial/jar/index.html)
Packages are defined like this in the Java Tutorial trail on interfaces and packages: "A collection of related classes and interfaces providing access protection and namespace management." (http://java.sun.com/docs/books/tutor...va/interpack/). While accurate, I don't find that this definition, by itself, really gives you a good sense of what a package is. For more information, go to the cited page and read within that section of the tutorial.