Creating JAR file in java from command prompt 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.
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 is jar command example to create jar file from command prompt, this will work both in windows and Linux operating system.
JAR command Examples in Java
added manifest
adding: HelloWorld.class(in = 450) (out= 311)(deflated 30%)
Manifest-Version: 1.0
Failed to load Main-Class manifest attribute from HelloWorld.jar
How to Create an executable JAR file in Java
Executable JAR File Example with External Manifest
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 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.
added manifest
adding: HelloWorld.class(in = 450) (out= 311)(deflated 30%)
Creating Executable JAR File By entry point
added manifest
adding: HelloWorld.class(in = 450) (out= 311)(deflated 30%)
Executing Java Program from JAR file
How to execute Java Program from Jar file
Executing Java Program from JAR file
How to view contents of a JAR file in Java
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
How to extract contents of JAR File
created: META-INF/
inflated: META-INF/MANIFEST.MF
inflated: HelloWorld.class
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.
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 to Set 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:
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
WAR and EAR - related JAR like fies in Java
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 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
How to convert String to Enum in Java
Difference between Thread and Runnable
How to Convert Double to String in Java
How to solve UnSupportedClassVersionError in Java
Difference between ClassNotFoundException and NoClassDefFoundError
How to fix ava.net.BindException: Address already in use: JVM_Bind:8080 Solution
How to convert ArrayList to HashSet in Java
No comments:
Post a Comment