Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » How to Create Executable or Normal Jar File in Java?

How to Create Executable Jar File in Java?
Objective:
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 times,I along with my friends use to think how to make my java program executable? Today I found out a solution how to make my java program executable.

Fortunately java has come up with java archive concept popularly known as .jar file. Even larger projects uses .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 objective of this post is show, 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)
  • Now 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: classname where the main class exists
  • press enter twice to make sure cursor is in the next line
  • A jar file created with a 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






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 a nice tutorials on how to create jar files in java.


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 work around 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


Lets 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:\myProgram\adder.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:\myProgram\AdderConsumer.java
//this will create .class file for the consumer class
java -cp c:\adder.jar E:\myProgram\AdderConsumer
The output will be-
 

Thats it.

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply