Jar file is distributable unit for j2se applications. It is a zipped file but with jar extension. Jar file contains following contents:
- Java compiled classes
- manifest.mf file which contains meta info regarding jar like name of main class, jars to be added in classpath etc, version info etc. (optional)
- other jars on which classes of this jar depends (optional)
There are two types of jars:
- Library Jar
- Executable Jar
Difference between the two jars is simply a main class. If your application runs standalone and contains main class, then you put name of main class in manifest file. This makes a jar an executable jar. You can run your executable jar just by double clicking on it or through java command with switch -jar. On the other hand, library jar does not contain any main class, and it always runs as a dependency jar for other java applications.
Now lets see how we can create a library jar
If you project just contains java classes and no other jars or manifest file, then you will create jar using following command:
project-folder>jar cvf mylibrary.jar -C classes .
cvf are three switches for jar. "c" means i want to create a new jar rather than updating previous one. "v" means i want to print all log info on console. "f" means supply an output to file i.e. mylibrary.jar in this case. -C is another switch which is used to tell which folders you want to include in jar. In my case all compiled java files are under classes folder but i don't want to include classes folder itself in jar so i used "-C classes .". Dot "." here tells jar command not to include classes folder. If you omit ".", then you have to change switch -C with this "-C classes/". it will tell jar command that all compiles files are under classes folder and include classes folder itself in jar file.
if you look at the contents of created jar file using any unzipped software, you will find that it contains classes + one more folder named as "META-INF". This folder contains manifest.mf. By default jar creates manifest.mf itself if you don't provide it. If you open this manfiest.mf using any text editor, you will see it just contains following two entries:
Manifest-Version: 1.0
Created-By: 1.6.0_03 (Sun Microsystems Inc.)
Important one is "Created-By" which tells which version of java was used to package jar file. In this case it is jse 6 with update 3.
If classes depend on other jars, you have to include those jars in classpath. This can be done at the time of running a program or writing all dependencis in manifest file, so java will automatically add it to classpath. Suppose above jar "mylibrary.jar" depends on other jars named as "other1.jar" and "other2.jar". Now to include these two jars in classpath through manifest file. Create a text file with any name let dependencies.txt and add following lines:
Class-Path: other1.jar other2.jar
Keep in mind that you have to provide spaced in between the every two jar file names.
Now you can add this manifest file to already created manifest file using following command:
project-folder> jar uvfm mylibrary.jar dependencies.txt
Console output will show that Manifest.mf updated. In this command i used switch "u" instead of "c" because i want to append manifest in already created jar. I also used "m" switch, which is used when you want to provide your own manifest file. If i want to create new jars with my own manifest file, then i have to use following command:
project-folder> jar cvfm mylibrary.jar dependencies.txt -C classes .
It is the same command as used previously to create jar with exception of extra "m" switch and name of manifest file.
Procedure of creating executable jar file is the same as library jar but you have to provide one extra attribute in your manifest file with name "Main-Class", So if i have name of main class as "org.engineers.java.MyMain", then i will have to added one entry in manifest file as:
Main-Class: org.engineers.java.MyMain
Showing posts with label package. Show all posts
Showing posts with label package. Show all posts
Saturday, July 5, 2008
Saturday, June 21, 2008
Compiling and Executing Java program
There are three basic steps in development of a simple java program which are Coding, Compiling, and Executing the java program. Coding is to write different set of classes and interfaces using any text editor like notepad etc. There are three cases which a java program can have. Those are:
Case A: Classes in default package (no package) and does not use classes which are bundled in jar.
Case B: Classes which are grouped in different packages but does not use classes which are bundled in jar.
Case C: Classes which are group in different packages and also uses classes bundled in jar.
Now i discuss compilation and execution of java program in each case one by one.
Lets discuss Case A. It is most simplest case which does not have any dependency on classes of others which are bundled in jar. Morever, classes are not grouped in different packages. Classes which are not grouped in packages, are said to be contained in default package. Suppose you have created a folder named as "TestProject" and has placed all java files there, then you can do following in windows to compile and exeuctge java program:
Just go to directory "TestProject" through command prompt and write following command:
C:\TestProject>javac *
above command will compile all java files. Now In order to run the java program, write following command:
C:\TestProject>java MainClass
where MainClass is any class which contains "public static void main(String[] args)" method.
Now we discuss Case B in which classes are grouped under packages. Packages provide better management of code, easy maintenance, and good access control on classes. Morever, packages provide safety from class name collissions. This case has further two possibilities. One is that you have logical grouped java classes in packages but placed them together like in case A i.e. all classes in folder TestProject. Now you will use following command to compile the code:
C:\TestProject>javac * -d .
"d" switch is used to provide destination for compiled classes. When destination is ".", it means root directory (TestProject in this case) will be default directory. javac command will first create folder heirarchy similar to packages and will place compiled class files in respective folder e.g. if a class is in package "org.engineers.project", then javac will first create folder named as "org" at root directory, then will create "engineers" folder under "org" and "project" folder under "engineers" folder. and will place class in "project" folder. In order to execute java program, you have to write following command at root directory:
C:\TestProject>java fully_qualified_class_name
fully_qualified_class_name means name of class with package e.g. if a class "Main" is contained under package org.engineers.project, then fully_qualified_class_name will be "org.engineers.project.Main"
Now we talk about second possibility in Case B which is that you have placed java source files in same folder hierarchy, as of packages i.e. if a class "Main.java" is in package "org.engineers.project", you have placed Main.java in folder org/engineers/project folder. You can use same command to compile and execute java program as in first possibility but it will mix your compiled code with source code so in order to keep them separate create a directory named as "src" under root directory (TestProject in this case) and then place all folder hierarchy containing source files there. Now create another folder named as "classes" at root level and use following command to compile the code:
C:\TestProject>javac * -d classes
"classes" folder be treated as root folder for class files.
In order to run java program, you have to change java command slightly. New command will be:
C:\TestProject>java -cp classes org.engineers.project.Main
Difference with this command and with previous command is of addition switch named as "cp". it represents classpath and tells java command where to find compiled classes. Since compiled classes are in "classes" folder, so i added that folder in classpath.
Now its time to discuss Case C, which is more realistic case. In this case, java source files uses classes which are placed in "jar" file. Jar file is just a zip file but with extensino java archive ("jar") . Now you will have to include all jar files in classpath using same switch which we used in preceding case to run the program. but this time we will have to use switch in both javac and java command.
C:\TestProject> javac -cp test.jar * -d classes
where test.jar is jar file which contains other classes which my classes use.
To execute the java program use following command:
C:\TestProject> java -cp test.jar;classes org.enigneers.project.Main
Note that i have added both "test.jar" and "classes folder in classpath.
Case A: Classes in default package (no package) and does not use classes which are bundled in jar.
Case B: Classes which are grouped in different packages but does not use classes which are bundled in jar.
Case C: Classes which are group in different packages and also uses classes bundled in jar.
Now i discuss compilation and execution of java program in each case one by one.
Lets discuss Case A. It is most simplest case which does not have any dependency on classes of others which are bundled in jar. Morever, classes are not grouped in different packages. Classes which are not grouped in packages, are said to be contained in default package. Suppose you have created a folder named as "TestProject" and has placed all java files there, then you can do following in windows to compile and exeuctge java program:
Just go to directory "TestProject" through command prompt and write following command:
C:\TestProject>javac *
above command will compile all java files. Now In order to run the java program, write following command:
C:\TestProject>java MainClass
where MainClass is any class which contains "public static void main(String[] args)" method.
Now we discuss Case B in which classes are grouped under packages. Packages provide better management of code, easy maintenance, and good access control on classes. Morever, packages provide safety from class name collissions. This case has further two possibilities. One is that you have logical grouped java classes in packages but placed them together like in case A i.e. all classes in folder TestProject. Now you will use following command to compile the code:
C:\TestProject>javac * -d .
"d" switch is used to provide destination for compiled classes. When destination is ".", it means root directory (TestProject in this case) will be default directory. javac command will first create folder heirarchy similar to packages and will place compiled class files in respective folder e.g. if a class is in package "org.engineers.project", then javac will first create folder named as "org" at root directory, then will create "engineers" folder under "org" and "project" folder under "engineers" folder. and will place class in "project" folder. In order to execute java program, you have to write following command at root directory:
C:\TestProject>java fully_qualified_class_name
fully_qualified_class_name means name of class with package e.g. if a class "Main" is contained under package org.engineers.project, then fully_qualified_class_name will be "org.engineers.project.Main"
Now we talk about second possibility in Case B which is that you have placed java source files in same folder hierarchy, as of packages i.e. if a class "Main.java" is in package "org.engineers.project", you have placed Main.java in folder org/engineers/project folder. You can use same command to compile and execute java program as in first possibility but it will mix your compiled code with source code so in order to keep them separate create a directory named as "src" under root directory (TestProject in this case) and then place all folder hierarchy containing source files there. Now create another folder named as "classes" at root level and use following command to compile the code:
C:\TestProject>javac * -d classes
"classes" folder be treated as root folder for class files.
In order to run java program, you have to change java command slightly. New command will be:
C:\TestProject>java -cp classes org.engineers.project.Main
Difference with this command and with previous command is of addition switch named as "cp". it represents classpath and tells java command where to find compiled classes. Since compiled classes are in "classes" folder, so i added that folder in classpath.
Now its time to discuss Case C, which is more realistic case. In this case, java source files uses classes which are placed in "jar" file. Jar file is just a zip file but with extensino java archive ("jar") . Now you will have to include all jar files in classpath using same switch which we used in preceding case to run the program. but this time we will have to use switch in both javac and java command.
C:\TestProject> javac -cp test.jar * -d classes
where test.jar is jar file which contains other classes which my classes use.
To execute the java program use following command:
C:\TestProject> java -cp test.jar;classes org.enigneers.project.Main
Note that i have added both "test.jar" and "classes folder in classpath.
Subscribe to:
Posts (Atom)