Tuesday, July 13, 2021

How to create and execute JAR file in Java – Command line Eclipse Netbeans Example

Creating JAR file in java from the 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 a whole. JAR file in Java is a kind of zip file that holds all contents of a Java application including Class files, resources such as images, sound files, and optional Manifest files. JAR stands for Java Archive and provides a platform-independent deliverable for java programs, libraries and frameworks. you can execute the 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 the internet especially 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 files, how to view contents of a jar file from command prompt and Eclipse and Netbeans.

 

How to create jar file in Java from the command prompt

Example to create and execute JAR file in Java from Command line Eclipse and Netbeansjar command in Java allows you to create a jar file from the command prompt, what is required is that you must have jar command included in the System PATH variable. you can check this by typing "jar" in the 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 a jar file in Java, the 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

javin@localhost:~/Java jar -cvf HelloWorld.jar HelloWorld.class
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
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

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 a jar file, the 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 an 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

1. Create a MANIFEST.MF file by using any text editor e.g. notepad in windows or Vim in Unix and add the following entry in the file, remember the 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 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%)

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 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 that resides in JDK_HOME/bin folder where JDK_HOME is the JDK installation directory. In order to use jar command from the command prompt, this bin folder must be in your System's PATH variable.

Don't worry if it's 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 the following output when you execute the jar command from the 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 files 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 Websphere. 

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 comparatively downloads faster than downloading individual components.

EAR file
EAR file stands for Enterprise Java Archive and is used to package an Enterprise Java application, like earlier WAR and JAR files. What separates the EAR archive from to a WAR file is the 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 deployable 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.


Other Java tutorials you may like

8 comments:

  1. jar -e switch is added on Java 6. Its a relatively new option and used to specify entry point i.e. Main-Class attribute in JAR's manifest file. I rarely use JAR command to create JAR file most of the time JAR is created using ANT build script or maven JAR plug-in. Though I understand having good knowledge of JAR command can help you lot while debugging or playing in JAVA environment and these examples of JAR command is very helpful.

    ReplyDelete
  2. creating JAR file in Java is much easier in IDE than command line. I suggest using Ecipse or Netbeans for creating JAR file. if you are building your project using ANT build.xml than you can use jar task to create JAR file and if you are using MAVEN to build your project you can use Maven JAR plugin for creating JAR. once you have JAR its pretty easy to run Java program. If your JAR file specifies Main-Class attribute than just double click the JAR file and it will execute, these are called executable JAR in Java but if JAR doesn't contain Main-Class attribute than you can specify main class name in command line while executing JAR command.

    ReplyDelete
  3. Amazing article, very good & through explanation of JAR, thanks for posting.

    Can you please provide some explanation around resource files in JAVA:
    Where should we put them?
    how should we access theme?
    As my program works well when I run it from Eclipse but does not work from command prompt, I also checked the JAR & it contains all the resources inside classes folder in the JAR.

    ReplyDelete
  4. In regards to what जिवन said, you will either need to specify the location of all your external resource files in the Classpath variable in either your manifest file or directly modifying the environment variable "CLASSPATH".

    I was having the same issue with my program. It runs fine off eclipse but if I try to run off command prompt it bombs. However, when I ran it on the server that will run the program (it has all the .jar referenced in the CLASSPATH environment variable) it ran just fine.

    ReplyDelete
  5. Hey Guy, I hope you could assist me here. Afte "clean and build" my project on netbeans, I couldn't find the dist folder. I discovered the path was C:\\Downloads\Documents\NetbeansProject(instead of the default path generated by netbeans. That is: C:\\Documents\NetbeansProject). Is it possible to go back to Netbeans and correct the error? How can I do that? Thanks

    ReplyDelete
  6. how to run classes which are from two different jar

    ReplyDelete
  7. nice write up. Fix path too long error while extracting a ZIP file.

    ReplyDelete
  8. it is not working properly

    ReplyDelete