UNIT-5 5.1 Jar Files in Java
UNIT-5 5.1 Jar Files in Java
Creation of a JAR file − The jar cf command can be used, where ‘cf’ means creating
the file.
Once a jar file has been created, a default manifest file is also created. Only one
manifest file is present in a specific archive, and it will have the extension ‘mf’ and
will be in the pathname. This
manifest file helps give information about the files that have been compressed/present
in the package.
Viewing a JAR file − The ‘jar tf’ command can be used to view the file that has been
compressed. Here, ‘tf’ represents viewing the table contents.
jar tf jar_file_name
Extracting contents from a jar file − The ‘xf’ command can be used to extract the
contents from a jar file. Here, ‘xf’ represents extraction.
jar xf jar_file_name
Updating the jar file − The ‘uf’ command is used to update, where ‘u’ represents
updating of contents of an existing jar file. This is done by modifying the manifest file
or by adding new files.
Running a jar file −If an application is compressed as a jar file, the below command
can be used.
The c and f options can appear in either order, but there must not be any space
between them.
This command will generate a compressed JAR file and place it in the current
directory. The command will also generate a default manifest file for the JAR archive.
Note:
The metadata in the JAR file, such as the entry names, comments, and contents of the
manifest, must be encoded in UTF8.
You can add any of these additional options to the cf options of the basic command:
Note:
When you create a JAR file, the time of creation is stored in the JAR file. Therefore,
even if the contents of the JAR file do not change, when you create a JAR file
multiple times, the resulting files are not exactly identical. You should be aware of
this when you are using JAR files in a build environment. It is recommended that you
use versioning information in the manifest file, rather than creation time, to control
versions of a JAR file. See the Setting Package Version Information section.
An Example
Let us look at an example. A simple TicTacToe applet. You can see the source code
of this applet by downloading the JDK Demos and Samples bundle from Java SE
Downloads. This demo contains class files, audio files, and images having this
structure:
The audio and images subdirectories contain sound files and GIF images used by the
applet.
You can obtain all these files from jar/examples directory when you download the
entire Tutorial online. To package this demo into a single JAR file named
TicTacToe.jar, you would run this command from inside the TicTacToe directory:
The audio and images arguments represent directories, so the Jar tool will
recursively place them and their contents in the JAR file. The generated JAR file
TicTacToe.jar will be placed in the current directory. Because the command used
the v option for verbose output, you would see something similar to this output when
you run the command:
You can see from this output that the JAR file TicTacToe.jar is compressed. The Jar
tool compresses files by default. You can turn off the compression feature by using
the 0 (zero) option, so that the command would look like:
You might want to avoid compression, for example, to increase the speed with which
a JAR file could be loaded by a browser. Uncompressed JAR files can generally be
loaded more quickly than compressed files because the need to decompress the files
during loading is eliminated. However, there is a tradeoff in that download time over
a network may be longer for larger, uncompressed files.
The Jar tool will accept arguments that use the wildcard * symbol. As long as there
weren't any unwanted files in the TicTacToe directory, you could have used this
alternative command to construct the JAR file:
Though the verbose output doesn't indicate it, the Jar tool automatically adds a
manifest file to the JAR archive with path name META-INF/MANIFEST.MF. See the
Working with Manifest Files: The Basics section for information about manifest files.
In the above example, the files in the archive retained their relative path names and
directory structure. The Jar tool provides the -C option that you can use to create a
JAR file in which the relative paths of the archived files are not preserved. It's
modeled after TAR's -C option.
As an example, suppose you wanted to put audio files and gif images used by the
TicTacToe demo into a JAR file, and that you wanted all the files to be on the top
level, with no directory hierarchy. You could accomplish that by issuing this
command from the parent directory of the images and audio directories:
The -C images part of this command directs the Jar tool to go to the images
directory, and the . following -C images directs the Jar tool to archive all the
contents of that directory. The -C audio . part of the command then does the same
with the audio directory. The resulting JAR file would have this table of contents:
META-INF/MANIFEST.MF
cross.gif
not.gif
beep.au
ding.au
return.au
yahoo1.au
yahoo2.au
By contrast, suppose that you used a command that did not employ the -C option:
META-INF/MANIFEST.MF
images/cross.gif
images/not.gif
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
5.2Internationalization and
Localization in Java
1. Internationalization and Localization
2. Understanding the culturally dependent data
3. Locale class
4. Example of Local class that prints the informations of the default locale
5. Example of Local class that prints english in different languages
6. Example of Local class that print display language of many locales
Messages
Dates
Times
Numbers
Currencies
Measurements
Phone Numbers
Postal Addresses
Labels on GUI components etc.
1. Locale(String language)
2. Locale(String language, String country)
3. Locale(String language, String country, String variant)
In this example, we are displaying the informations of the default locale. If you want
to get the informations about any specific locale, comment the first line statement and
uncomment the second line statement in the main method.
1. import java.util.*;
2. public class LocaleExample {
3. public static void main(String[] args) {
4. Locale locale=Locale.getDefault();
5. //Locale locale=new Locale("fr","fr");//for the specific locale
6.
7. System.out.println(locale.getDisplayCountry());
8. System.out.println(locale.getDisplayLanguage());
9. System.out.println(locale.getDisplayName());
10. System.out.println(locale.getISO3Country());
11. System.out.println(locale.getISO3Language());
12. System.out.println(locale.getLanguage());
13. System.out.println(locale.getCountry());
14.
15. }
16. }
5.3Java Swing
Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create
window-based applications. It is built on the top of AWT (Abstract Windowing
Toolkit) API and entirely written in java.
The javax.swing package provides classes for java swing API such as JButton,
JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
There are many differences between java awt and swing that are given below.
What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.
Do You Know
The methods of Component class are widely used in java swing that are given below.
Method Description
add a component on another
public void add(Component c)
component.
public void setSize(int
sets size of the component.
width,int height)
public void sets the layout manager for the
setLayout(LayoutManager m) component.
public void setVisible(boolean sets the visibility of the
b) component. It is by default false.
We can write the code of swing inside the main(), constructor or any other method.
Let's see a simple swing example where we are creating one button and adding it on
the JFrame object inside the main() method.
File: FirstSwingExample.java
1. import javax.swing.*;
2. public class FirstSwingExample {
3. public static void main(String[] args) {
4. JFrame f=new JFrame();//creating instance of JFrame
5.
6. JButton b=new JButton("click");//creating instance of JButton
7. b.setBounds(130,100,100, 40);//x axis, y axis, width, height
8.
9. f.add(b);//adding button in JFrame
10.
11. f.setSize(400,500);//400 width and 500 height
12. f.setLayout(null);//using no layout managers
13. f.setVisible(true);//making the frame visible
14. }
15. }