Re: what's the use of JAR files
Sreenivas wrote:
I am new to java , wats the use of jar files.wats the relation
between .zip and .jar files
JAR is an acronym for Java Archive. The Sun Java Tools site has a bunch of
documentation about JARs and the jar command:
<http://java.sun.com/javase/6/docs/technotes/tools/index.html#basic>
<http://java.sun.com/javase/6/docs/technotes/guides/jar/index.html>
JAR files have essentially the same internal structure as ZIP files. They
serve a special purpose in the Java universe, so they have a few extras, like
configuration files (MANIFEST.MF) buried inside them.
JAR files are managed at the command line by the Java 'jar' tool, an
executable utility program.
They are scripted via the deployment tools Ant or Maven.
When you compile or run Java code, you tell the Java engine about a
"classpath" that holds references to all the bytecode needed for the program.
For example, on file systems the bytecode resides in .class files in a
directory tree. The tree structure matches the package structure of the
classes inside it. The top (sub)directory of a classpath tree is the
"unnamed" or "default" package - each layer below that root is like one dot
level in a package name. Directory: com/lewscanon/example/foo/. Package:
com.lewscanon.example.foo.
Now you have a directory tree at some level in your hard drive, let's say it
sits at $PROJECTS/foo. ($PROJECTS, or %PROJECTS% in Windows terms, is an
environment variable for your projects directory. The project of interest is
the 'foo' project.)
Under 'foo/' (relative to the project directory), you have com/,
com/yourbusiness/, com/yourbusiness/slicedbread/ and
com/yourbusiness/slicedbread/foo/. Each directory layer is like a package
layer in the com.yourbusiness.slicedbread.foo set of namespaces.
In a JAR file you have that relative directory tree all packaged up, starting
at com/.
Now you can make that JAR file the classpath, instead of the directory tree.
$ java -classpath foo.jar com.yourbusiness.slicedbread.foo.SlicedBread
This command will invoke the main() method of the SlicedBread class within
that hierarchy inside the JAR.
It makes it easier to deploy an application or a library when you can put all
the related packages and classes (bytecode) inside a single JAR or a set of
closely related JARs. Put the JARs in the classpath of a program that needs
them, and away you go.
--
Lew