25
Create an executable JAR file on VS Code n Command line
The Jar (Java Archive) tool of JDK used to package one or more And it provides the facility to create the executable jar file which calls the main method of the class if you double click it.
Java class files
and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform Simply speaking, this is a format for archiving data. This is similar to you using WinRAR or WinZIP.
You can read Oracle documentation on jar here
You can read Oracle documentation on jar here
We will learn how to create an executable jar file, we will take a Java application and explore two ways to run it as a jar, just by double clicking on it. Using VsCode & Command line
What does it mean when we say that the file is an executable JAR file?
Well, when you double click on the JAR file, it automatically calls the main method of the program And if program have JAVA GUI such as
Well, when you double click on the JAR file, it automatically calls the main method of the program And if program have JAVA GUI such as
frames
, panel present inside the main method, that would also be executed.VS Code is a more flexible IDE in today’s competition, so its important to know to create jar file here which will get our job done faster with ease.
If you want to learn more about VsCode and other text editors you can go through this Blogpost
NOW, Follow the following steps:-
In VsCode marketplace install extension Java Extension Pack which includes set of extensions needed for configuring java enviroment in vscode or you can just download Project Manager for Java extension which is just needed here


In order to compile the code by packing JAR inside Vscode, at bottom left corner you will find and option

IMP: now its a very important task it will ask you to specify
this class provide the entry point so its important to mention.
JAVA PROJECTS
there you will find and symbol saying EXPORT JAR
. Click it
IMP: now its a very important task it will ask you to specify
main class
just provide the main class ,here MyCalc
this class provide the entry point so its important to mention.
NOTE:-Otherwise the jar file will become, normal jarfile not an executable ones

NOW using command line you should know the jar tool ,provides certain
some of them are as follows:
switches
with which we can create an executable jarfilesome of them are as follows:
- c creates new archive file
- v generates verbose output. It displays the included or extracted resource on the standard output.
- m includes manifest information from the given mf file.
- f specifies the archive file name
- x extracts files from the archive file
Write the java file and then and then follow the following step:-
We can do this with
javac
from the command line:javac MyCalc.java
The
javac
command creates MyCalc.class
in the current directory. If you have multiple java file compile them too, We can now package that into a jar file.MyCalc
) then press enter.NOTE:It's important that we end our manifest file with a newline.Otherwise no main manifest attribute error is thrown.
manifest.MF

Open the Command Prompt, write the command using jar tool switches provided
using jar
command
jar -cvfm <jarfilename.jar> <manifestfile> <classname.class>
Command order shouldn't be change
1.
2.switch
3.switch
4.switch
5.switch
Hence, the corresponding filename are also written in the same order ,and if there is multiple class files, then include them too.
1.
jar
command to create a jar file.2.switch
c
used to indicates we are creating new file3.switch
v
generates verbose output information4.switch
f
tell about the jarfile name we are creating 5.switch
m
it includes the manifest informationHence, the corresponding filename are also written in the same order ,and if there is multiple class files, then include them too.

NOW ,we can use the
-jar
option of the java command to run our application since executable jarfile has been created.java -jar CALC.jar
And that's it. Now you can also run your jarfile with an ease just by clicking it.
If you would like to download & run my
as to show how one can easily share ,justifying its properties that import anywhere it is required.
AND
If you would like to download & run my
CALC.jar
you can find hereas to show how one can easily share ,justifying its properties that import anywhere it is required.
AND
With this I will end my articles here,
Hope you all find it valuable, and if you have any doubt ,then you ask me just by commenting below.
Hope you all find it valuable, and if you have any doubt ,then you ask me just by commenting below.
25