Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

UNIT-5

5.1 Jar files in Java


JAR is a file format that helps in aggregating Java class file, along with its associated
metadata and resources into a single file.

Creation of a JAR file − The jar cf command can be used, where ‘cf’ means creating
the file.

jar cf jar_file_name files_to_compress

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.

jar uf jar_file_name other_input_files

Running a jar file −If an application is compressed as a jar file, the below command
can be used.

FILE_LOCATION:\>java –jar file_name.jar

Creating a JAR File


The basic format of the command for creating a JAR file is:

jar cf jar-file input-file(s)


The options and arguments used in this command are:

 The c option indicates that you want to create a JAR file.


 The f option indicates that you want the output to go to a file rather than to stdout.
 jar-file is the name that you want the resulting JAR file to have. You can use any
filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this
is not required.
 The input-file(s) argument is a space-separated list of one or more files that you want
to include in your JAR file. The input-file(s) argument can contain the wildcard *
symbol. If any of the "input-files" are directories, the contents of those directories are added
to the JAR archive recursively.

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:

jar command options


Option Description
Produces verbose output on stdout while the JAR file is being
v built. The verbose output tells you the name of each file as
it's added to the JAR file.
0
Indicates that you don't want the JAR file to be compressed.
(zero)
Indicates that the default manifest file should not be
M
produced.
Used to include manifest information from an existing
manifest file. The format for using this option is:
jar cmf jar-file existing-manifest input-file(s)
See Modifying a Manifest File for more information about this
m option.

Warning: The manifest must end with a new line or carriage


return. The last line will not be parsed properly if it does
not end with a new line or carriage return.

-C To change directories during execution of the command. See


below for an example.

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:

TicTacToe folder Hierarchy

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:

jar cvf TicTacToe.jar TicTacToe.class audio images

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:

adding: TicTacToe.class (in=3825) (out=2222) (deflated 41%)


adding: audio/ (in=0) (out=0) (stored 0%)
adding: audio/beep.au (in=4032) (out=3572) (deflated 11%)
adding: audio/ding.au (in=2566) (out=2055) (deflated 19%)
adding: audio/return.au (in=6558) (out=4401) (deflated 32%)
adding: audio/yahoo1.au (in=7834) (out=6985) (deflated 10%)
adding: audio/yahoo2.au (in=7463) (out=4607) (deflated 38%)
adding: images/ (in=0) (out=0) (stored 0%)
adding: images/cross.gif (in=157) (out=160) (deflated -1%)
adding: images/not.gif (in=158) (out=161) (deflated -1%)

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:

jar cvf0 TicTacToe.jar TicTacToe.class audio images

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:

jar cvf TicTacToe.jar *

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:

jar cf ImageAudio.jar -C images . -C audio .

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:

jar cf ImageAudio.jar images audio

The resulting JAR file would have this table of contents:

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

Internationalization is also abbreviated as I18N because there are total 18 characters


between the first letter 'I' and the last letter 'N'.

Internationalization is a mechanism to create such an application that can be adapted


to different languages and regions.

Internationalization is one of the powerful concept of java if you are developing an


application and want to display messages, currencies, date, time etc. according to the
specific region or language.

Localization is also abbreviated as I10N because there are total 10 characters


between the first letter 'L' and last letter 'N'. Localization is the mechanism to create
such an application that can be adapted to a specific language and region by adding
locale-specific text and component.

Understanding the culturally dependent data


before starting internationalization
Before starting the internationalization, Let's first understand what are the
informations that differ from one region to another. There is the list of culturally
dependent data:

 Messages
 Dates
 Times
 Numbers
 Currencies
 Measurements
 Phone Numbers
 Postal Addresses
 Labels on GUI components etc.

Importance of Locale class in


Internationalization
An object of Locale class represents a geographical or cultural region. This object can
be used to get the locale specific information such as country name, language, variant
etc.

Fields of Locale class


There are fields of Locale class:

1. public static final Locale ENGLISH


2. public static final Locale FRENCH
3. public static final Locale GERMAN
4. public static final Locale ITALIAN
5. public static final Locale JAPANESE
6. public static final Locale KOREAN
7. public static final Locale CHINESE
8. public static final Locale SIMPLIFIED_CHINESE
9. public static final Locale TRADITIONAL_CHINESE
10. public static final Locale FRANCE
11. public static final Locale GERMANY
12. public static final Locale ITALY
13. public static final Locale JAPAN
14. public static final Locale KOREA
15. public static final Locale CHINA
16. public static final Locale PRC
17. public static final Locale TAIWAN
18. public static final Locale UK
19. public static final Locale US
20. public static final Locale CANADA
21. public static final Locale CANADA_FRENCH
22. public static final Locale ROOT

Constructors of Locale class


There are three constructors of Locale class.They are as follows:

1. Locale(String language)
2. Locale(String language, String country)
3. Locale(String language, String country, String variant)

Commonly used methods of Locale class


There are given commonly used methods of Locale class.

1. public static Locale getDefault() it returns the instance of current Locale


2. public static Locale[] getAvailableLocales() it returns an array of available locales.
3. public String getDisplayCountry() it returns the country name of this locale object.
4. public String getDisplayLanguage() it returns the language name of this locale object.
5. public String getDisplayVariant() it returns the variant code for this locale object.
6. public String getISO3Country() it returns the three letter abbreviation for the current locale's
country.
7. public String getISO3Language() it returns the three letter abbreviation for the current
locale's language.

Example of Local class that prints the informations of the


default locale

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. }  

download this example


Output:United States
English
English (United States)
USA
eng
en
US

 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.

Unlike AWT, Java Swing provides platform-independent and lightweight


components.

The javax.swing package provides classes for java swing API such as JButton,
JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

Difference between AWT and Swing

There are many differences between java awt and swing that are given below.

No. Java AWT Java Swing


AWT components are platform- Java swing components are
1)
dependent. platform-independent.
Swing components are
2) AWT components are heavyweight.
lightweight.
AWT doesn't support pluggable look Swing supports pluggable
3)
and feel. look and feel.
Swing provides more powerful
components such as tables,
AWT provides less components than
4) lists, scrollpanes,
Swing.
colorchooser, tabbedpane
etc.
5) AWT doesn't follows MVC(Model View Swing follows MVC.
Controller) where model represents
data, view represents presentation
and controller acts as an interface
between model and view.

What is JFC

The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.

Do You Know

 How to create runnable jar file in java?


 How to display image on a button in swing?
 How to change the component color by choosing a color from ColorChooser ?
 How to display the digital watch in swing tutorial ?
 How to create a notepad in swing?
 How to create puzzle game and pic puzzle game in swing ?
 How to create tic tac toe game in swing ?

Hierarchy of Java Swing classes

The hierarchy of java swing API is given below.


Commonly used Methods of Component class

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.

Java Swing Examples


There are two ways to create a frame:

 By creating the object of Frame class (association)


 By extending Frame class (inheritance)

We can write the code of swing inside the main(), constructor or any other method.

Simple Java Swing Example

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. }  

You might also like