Creating JAR file in java from command prompt
is always been little tricky for many of us even if IDE like Netbeans andEclipse 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 jarfile from command prompt and Eclipse and Netbeans.
Read more: http://javarevisited.blogspot.com/2012/03/how-to-create-and-execute-jar-file-in.html#ixzz2khcu4Wd1
How to create jar file in Java form command prompt
jar 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 isjar command example to create jar file from command prompt, this will work both in windows and Linux operating system.
JAR command Examples in Java
javin
@localhost
:~/Java
jar -cvf HelloWorld.jar HelloWorld.class
added manifest
adding : HelloWorld .class (in = 450 ) (out = 311 )(deflated 30 % )
added manifest
adding : HelloWorld .class (in = 450 ) (out = 311 )(deflated 30 % )
This command will crate 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
Manifest-Version: 1.0
Created-By: 1.6.0-beta2 (Sun Microsystems Inc.)
This jar can not 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
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.
How to Create an executable JAR file 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 seeexample of both ways to create executable jar file in Java.
Executable JAR File Example with External Manifest
1.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
Main -Class : HelloWorld
Important thing to remember is that we need to specified fullclassified 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 its 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.
2.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 % )
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.
Creating Executable JAR File By entry point
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 isexample 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 % )
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
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 isjar 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
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
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
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 isstep by step guide of creating JAR file from Eclipse IDE: In Jar filemain 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 whereJDK_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
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 yourServlet, 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 likeTomcat 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 eitherwinzip 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.
Read more: http://javarevisited.blogspot.com/2012/03/how-to-create-and-execute-jar-file-in.html#ixzz2khcu4Wd1