What
is JAVA?
Originally known as
oak, Java is a programming language developed by James Gosling and others at
Sun Microsystems. It was first introduced to the public in 1995 and is widely
used to create Internet applications and other software programs. Today, Java is
maintained and owned by Oracle.
When used on the
Internet, Java allows applets to be downloaded and used through a browser,
which enables the browser to perform a function or feature not normally
available. Unlike JavaScript, the users must download or install the applet or
program before being able to utilize the Java program.
Below is an example
of a Java applet from Sun and a method of testing if Java is installed on your
computer. If Java is installed on your computer, you should see additional
information about the installed Java version and your operating system.
Java
is also used as the programming language for many different software programs,
games, and add-ons. Some examples of the more widely used programs written in
Java or that use Java include the Adobe Creative suite, Eclipse, Lotus Notes,
Minecraft, OpenOffice, Runescape, and Vuze.
What
is JAR?
A JAR (Java ARchive)
file is a file that contains the class, image, and sound files for a Java
application or applet gathered into a single file and possibly compressed. When
a programmer gets a Java program development kit, a small program or utility
called "jar" is included. The jar utility lets the programmer create,
list, or extract the individual files from a JAR file. In an enterprise, a Java
application can be started with a set of JAR files for use during execution. An
off-the-shelf open source package can be delivered as a JAR file and run with
XML data.
On the Web, a JAR
file containing an applet may accompany a Web page. By putting the applet
components in a single file and compressing that file, download time is saved.
Ordinarily, a browser user will not need to "open" or view a JAR file
directly. It is opened when the Web page is received and the applet is in some
manner initiated.
The JAR format is
based on the popular zip file format and is somewhat comparable to the Unix tar
file.
Creating JAR file in java from
command prompt
It is always been
little tricky for many of us even if IDE like Netbeans and Eclipse provide
support to export java program as JAR file simply because we don’t create jar
often and not familiar with manifest file or jar command as whole. JAR file in
Java is a kind of zip file which holds all contents of a Java application
including Class files, resources such as images, sound files and optional
Manifest file. JAR stands for Java Archive and provides a platform independent
deliverable for java programs, libraries and framework. you can execute same
jar file in any operating system.
E.g. Windows 7, windows 8, Macintosh or Linux.
Apart from platform independence and standard delivery method jar file also
provides compression of contents which results in faster download if you are
downloading java program from internet specially in case of mobile devices
where you install Java program by OTA. In this article we will some JAR command
examples and learn how to create and execute jar file, how to view contents of
jar file from command prompt and Eclipse and Netbeans.
Example to create and
execute JAR file in Java from Command line Eclipse and Netbeansjar command in
Java allows you to create jar file from command prompt, what is required is
that you must have jar command included in System PATH variable. you can check
this by typing "jar" in command prompt if it doesn't throw error as
"jar is not recognized as an internal or external command" they you
are ready to go. When you create jar file in Java, command also creates
Manifest file which is optional and you can control whether to create it or not
by jar command line options, but if you want to create executable jar file they
you must need Manifest file which we will discuss in further sections. Now here
is jar command example to create jar file from command prompt, this will work
both in windows and Linux operating system.
AR command Examples in Java
javin@localhost:~/Java
jar -cvf HelloWorld.jar HelloWorld.class
added
manifest
adding:
HelloWorld.class(in = 450) (out= 311)(deflated 30%)
This command will create
Helloworld. jar which contains Helloworld.class file. this will also create
manifest file but without Main-Class entry as shown below:
javin@localhost:~/Java
cat MANIFEST.MF
Manifest-Version:
1.0
Created-By:
1.6.0-beta2 (Sun Microsystems Inc.)
This jar cannot be
executed and you will get error when you try to run this jar file:
javin@localhost:~/Java java -jar
HelloWorld.jar
Failed to load
Main-Class manifest attribute from HelloWorld.jar
You just need to
provide Main-Class entry to get rid of this error which we will see in coming
Section.
AR command Examples in Java
To create an executable
JAR in Java, you need to provide a manifest file and include your Main Class in
Manifest. When you create jar file , jar command also creates manifest file
inside META-INF as MANIFEST.MF but doesn't create Main-Class entry which is
required for executable jar file. You can create executable jar file in Java by
two ways either provide a self created Manifest file or specify entry point
using "-e" jar option. If you provide external Manifest file than you
need to use jar -m option to include that manifest file inside jar. Let's see
example of both ways to create executable jar file in Java.
Executable JAR File Example with
External Manifest
Create MANIFEST.MF
file by using any text editor e.g. notepad in windows or Vim in Unix and add
following entry in file, remember last line must end with either new line or
carriage return:
Manifest-version:
1.0
Main-Class:
HelloWorld
Important thing to
remember is that we need to specified full classified class name here. suppose
if our main class was inside com/example/HelloWorld than we should have to
specify com.example.HelloWorld here, don't put .class extension here it’s not
required. Apart from specifying Main-Class you can also specify Java Classpath
in Manifest file which is important if your application is depended on external
library jars. "Classpath" entry supersede both -cp and CLASSPATH
environment variable. to learn more see How ClassPath works in Java.
Execute following jar
command to create executable jar
javin@localhost:~/Java
jar -cvfm HelloWorld.jar MANIFEST.MF HelloWorld.class
added
manifest
adding:
HelloWorld.class(in = 450) (out= 311)(deflated 30%)
here -m is used for
including manifest file and remember specify name of manifest file after jar
name. now you have an executable jar file in java which you run by command
specified earlier.
Executable JAR File Example with
External Manifest
This seems to me an
easy way to create executable jars in Java, as you need not have to create
manifest file explicitly and it will be create by jar command itself along with
Main-Class entry. What you need to provide is a new jar option "-e"
and you main class name while running jar command. here is example of jar
command with entry option:
javin@localhost:~/Java
jar -cvfe HelloWorld.jar HelloWorld HelloWorld.class
added
manifest
adding:
HelloWorld.class(in = 450) (out= 311)(deflated 30%)
jar -e for entry
point and entry point or main class name should come after jar file name and
before directory or file needs to be included in JAR. You can now run your
executable jar file by issuing "java -jar" command as shown in
following example:
javin@localhost:~/Java
java -jar HelloWorld.jar
Executing Java Program from JAR
file
How
to execute Java Program from Jar file
Executing jar program
from jar archive is very easy one thing required is jar must be executable and
must have Main-Class entry in MANIFEST.MF file. here is a Java command example
for running java program from jar file:
javin@localhost:~/Java
java -jar HelloWorld.jar
Executing
Java Program from JAR file
Here we have
specified jar file name with -jar option and it will run main class declared as
“Main-Class” attribute in manifest file.
How to view contents of a JAR file
in Java
Jar command in Java
allows you to view files and directories inside of a jar file without
extracting or unzipping original jar. "-t" jar option is used to list
files from jar archive as shown in jar command example below:
javin@localhost:~/Java
jar -tvf HelloWorld.jar
0
Wed Dec 07 22:36:12 VET 2011 META-INF/
95
Wed Dec 07 22:36:12 VET 2011 META-INF/MANIFEST.MF
450
Wed Dec 07 21:36:04 VET 2011 HelloWorld.class
here "-t" for listing
and "-v" and "-f" for verbose and jar file name.
How to extract contents of JAR File
use jar option
"-v" for extracting files form JAR files as shown in jar command
example below:
javin@localhost:~/Java
jar -xvf HelloWorld.jar
created:
META-INF/
inflated:
META-INF/MANIFEST.MF
inflated:
HelloWorld.class
here -x for
extracting , -v is for verbose and -f specify jar file name.
How to create jar file in Eclipse
Creating JAR file
in Eclipse IDE is a cakewalk once you
know the process. here is step by step guide of creating JAR file from Eclipse
IDE: In Jar file main class is specified as “Main-Class” attribute inside
manifest file and used as program entry point if you double click on JAR or run
jar from java command.
1) Select Project for
which you want to create jar file.
2) Go to File Menu
and select Export
3) Expand Java folder
and select JAR file
Now you just need to
click next and follow instruction as displayed. you can select what contents
you want to export to jar file and specify Main Class entry as well. If you
like Eclipse IDE then you may like my earlier post on eclipse as well e.g. Java
debugging tips in Eclipse and How to
setup java remote debugging in Eclipse.
How to create jar file in Netbeans
In Netbeans to create
jar file you need to build the project which execute project ant file and
creates JAR file inside dist folder. You can go on properties of project and
specify main class there which will be run when you run the project and same
will be used to create “Main-Class” attribute in JAR file.
jar is not recognized as an
internal or external command
If you get this error
while executing jar command from command prompt in Windows or Unix it means
your Java Path is not set properly. JAR command is a binary which resides in
JDK_HOME/bin folder where JDK_HOME is JDK installation directory. In order to
use jar command from command prompt this bin folder must be in your System's
PATH variable. Don't worry if its not there in PATH you can check this link
toSet PATH for Java in Windows and Unix.It shows how you can do it in both
Windows and Unix. Once your PATH is property set, you will see following output
when you execute jar command from command
line:
javin@localhost:~
jar
Usage:
jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
And now you are ready
to use jar from command prompt.
WAR and EAR - related JAR like fies in Java
WAR
file
WAR file in Java
stands for Web application archive and it is used to package a Java application
together you can package all your Servlet, JSP, CSS, images, html in one WAR
file and then deploy it to any Java web or application server like Tomcat,
Weblogic or webshere. WAR files provide a clean and faster way to package and
deploy Java web application just like JAR file provides for core java apps.
Since WAR file also compacts resources inside it is comparatively download
faster than downloading individual components.
EAR
file
EAR file stands for
Enterprise Java Archive and used to package an Enterprise Java application,
like earlier WAR and JAR file. What separates EAR archive to WAR file is
inclusion of Enterprise Java Beans(EJB). EAR file contains all web resources
including Servlet, JSP, html, javascript, css, images along-with EJB. You can
not deploy EAR files into web servers like Tomcat because it doesn't support
EJB and can only be deploy-able in Application servers like WebSphere or
Weblogic.
JAR File format in Java
Few words about java
jar file format, its similar to zip format and use .jar extension. you can open
JAR file in windows by using either winzip or winrar zip utilities.
That’s all on how to
create jar file from command line, Eclipse, Netbeans. How to extract contents,
how to run Java program from jar file etc. Let me know if you face any issue
while creating JAR file in java.
Sign up here with your email
ConversionConversion EmoticonEmoticon