How to Create Executable or Normal Jar File in Java?

Executable or Normal Jar File in Java

Introduction to Executable or Normal Jar File in Java

It is nice to see my coded program is up and running on my desktop. I can send it to anybody to make them use my program. I don’t need command prompt to run the java program.

Most of the time, I along with my friends use to think about how to make my java program executable? Today I found a solution on how to make my java program executable.

Ads code goes here

We do not need to create the same file structure at all the customer locations. JAR automatically restores the file structure. As the JAR is already compressed, it is easy to download our network.

Fortunately, Java has come up with java-archive concept popularly known as .jar file. Even larger projects use .jar files. We can create a .jar file from one class or from several other classes.

Jar files are created with the utility called jar.exe of JDK

So the objective of this post is shown, how we can create .jar file from our existing program step by step.

Steps:

 

    • Compile all of your classes that need to be archived
    • Step-1 will create corresponding .class files in the folder(in the corresponding folder)
    • No need to create a manifest file which will tell the jar.exe where the main class exists.As main class is the starting point to trigger our program
    • To create a manifest file, create a .txt file named as “myManifest.txt”
    • Type Main-class: class name where the main class exists

  • Press enter twice to make sure the cursor is in the next line
  • A jar file created with the main class manifest can be used both as a library and a runnable jar.  If you use it as a library, you can edit and compile any of the classes included in the jar, and add it to your project.  Then it will override the one in the jar file
  • jar cvfm MyJarName.jar manifest.txt *.class(if you want to add of your classes of the folder)
  • jar cvfm MyJarName.jar manifest.txt name of the single.class
READ  File Concept Simplified In Java

Jar’s different parameters:
Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir]

 

Options:
    -c  create new archive
    -t  list table of contents for archive
    -x  extract named (or all) files from archive
    -u  update existing archive
    -v  generate verbose output on standard output
    -f  specify archive file name
    -m  include manifest information from specified manifest file
    -e  specify application entry point for stand-alone application
        bundled into an executable jar file
    -0  store only; use no ZIP compression
    -M  do not create a manifest file for the entries
    -i  generate index information for the specified jar files
    -C  change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.

Here is the link on how to create jar files in different editors.

So to create a jar file the command will be-

jar -cvf |name of the jarfile.jar| |class name1| |class name2|
jar -cvf myjar.jar Test1.class Test2.class
or
// to put all class files
jar -cvf myjar.jar *.*

How to extract a jar file??

jar -xvf myjar.jar

Please do remember if you are trying to access the content of the jar, you need to put the jar file to the classpath.

How to display table of content of a jar file?

jar -tvf myjar.jar

If you do not want to put in the class file there is a workaround for you. Please put the jar file to the below-written path– by default it will be available to compiler and JVM.
Path–>
JDK–>JRE–>lib–>ext–>*.jar

Let’s take one example–
let us design a class file where we will have two methods add and mul(multiply)

public class adder
{
public static int add(int a,int b)
{
//calculate addition
return a+b;
}
public static int mul(int a,int b)
{
//calculate multiplication
return a*b;
}
}

Now we will compile this class using javac command
C:>javac E:myProgramadder.java–this will create a .class file in the classpath

jar -cvf adder.jar adder.class

This will create a jar file in the present working directory…

 

Now let us design the client class which will consume this adder jar
 

public class AdderConsumer
{
public static void main(String...Args)
{
System.out.println(adder.mul(2,3));
System.out.println(adder.add(2,3));
}
}

Compilation Process

javac -cp C:adder.jar E:myProgramAdderConsumer.java
//this will create .class file for the consumer class
java -cp c:adder.jar E:myProgramAdderConsumer

The output will be-
 

READ  ENUM Concept Simplified In Java

Difference Between JAR, WAR, and EAR

If there are many files and they are dependent on nature for your program. It is very tough to put them in the classpath to access them run time.

For solving this problem java has come up with an archiving concept. Its concept is simple. put all the classes in a zip file and make that available in classpath while running the java code.

Again consider one more example, if our program contains many HTML files, images files, etc. so to download one single file, it is required to download all the components of the file.

And for that, we require so many HTTP connections to get all those. It will take more time and patience. so the zip file is a nice concept.

Mainly we have 3 types of zip files in java.

      1. jar (java archive file)
      2. war (web archive file)
      3. ear (enterprise archive file)

jar(Java Archive Tool)

Syntax for jar

jar c|t|x[f] [m] [v] [jar-file] [manifest-file] [files]

Jar’s details

jar is a tool that can be used to create and manipulate Java Archive (JAR) files. A JAR file is a compressed ZIP file with an additional ” manifest ” file. The syntax of the jar command is reminiscent of the Unix tar (tape archive) command.
Options to jar are specified as a block of concatenated letters passed as a single argument, rather than as individual command-line arguments.

The first letter of this option argument specifies what action jar is to perform and is required. Other option letters are optional. The various file arguments depend on which
option letters are specified.

Further visit: Features of Java

jar Options

The first argument to jar is a set of letters that specifies the operation it is to perform. The first letter
specifies the basic action and is required. The three possible values are the following:

      • c-Create a new JAR archive. A list of input files and/ or directories must be specified as the final arguments to jar.
      • t-List the contents of a JAR archive. If a JAR file is specified with the f option, its contents are listed. Otherwise, the JAR file to be listed is read from standard input.
      • x-Extract the contents of a JAR archive. If a JAR file is specified with the f option, its contents are extracted. Otherwise, the JAR file to be extracted is read from standard input. If the command is followed by a list of files and/ or directories, only those files or directories are extracted from the JAR archive. Otherwise, all files in the archive are extracted.
      • f-optional command letter. This option indicates that the JAR file to create, list, or extract from is specified on the command line. When f is used with c, t, or x, the JAR filename must be the second command-line argument to the jar (ie, it must follow the block of option letters). If this option is not specified, jar writes the JAR file it creates to standard output or reads a JAR file from standard input.
      • m-optional command letter. This option is only used with the c action. It indicates that jar should read the manifest file (partial or complete) specified on the command line and use that file as the basis for the manifest it includes in the JAR file. When this argument is specified after the f option, the manifest filename should follow the destination filename. If m precedes the f option, the manifest filename should precede the destination filename.
      • v-optional command letter and it is called Verbose. If this letter is specified with a c action, jar lists each file it adds to the archive with compression statistics. If it is used with a t action, jar lists the size and modification date for each file in the archive, instead of simply listing the filename. If v is used with x. jar displays the name of each file it extracts from the archive.
READ  Learn Methods in Java in a Quick Manner

Example of switches in jar


%jar cvf myJarFile.jar *.java pictures //creates a simple jar
%jar tvf myJarFile.jar //lists the contents of the jar
%jar xf  myJarFile.jar META-INF/MANIFEST.MF //extracts the manifest file from jar
%jar cfmv myJarFile.jar manifest.stub test/beans/myFiles//creates a jar with a partial manifest specified

Let’s check out the difference between them:

JarWarEar
  1. it contains mostly web application resources like HTML,CSS,JSP,Servlets,images,beans etc
  2. It is having web.xml to configure
  3. It helps to maintain, deployment and shipping very easy for web application
  4. extension .war
  1. We use it on enterprise application. mainly combination of wars and jars
  2. It is having XML based deployment descriptor to configure all
  3. It helps to maintain, deployment and shipping very easy for enterprise  application
  4. extension .ear
  1. contains .class files
  2.  A JAR file encapsulates one or more Java classes, a manifest, and a descriptor.
  3. JAR files are the lowest level of archive
  4. It is having MANIFEST.MF in META-INF folder
  5. extension .jar

Share and Enjoy !

4 thoughts on “How to Create Executable or Normal Jar File in Java?”

  1. Your home is valueble for me. Thanks!?This website is mostly a stroll-via for the entire info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll undoubtedly discover it.

  2. I’ve been exploring for a little bit for any high quality articles
    or weblog posts in this kind of house . Exploring in Yahoo I
    eventually stumbled upon this site. Studying this info So i’m happy to show that I’ve a very excellent uncanny feeling I
    discovered just what I needed. I such a lot indisputably will make sure to don?t overlook this website and give it a
    look on a relentless basis.

  3. Hi there! This is kind of off topic but I need some help from an established blog.
    Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about creating my own but I’m not sure where to begin.
    Do you have any ideas or suggestions? Appreciate it

Leave a Comment

Your email address will not be published. Required fields are marked *