0% found this document useful (0 votes)
11 views

Ajava Unit V

The document discusses creating and working with JAR files in Java. It explains how to create a JAR file using the jar command, view the contents of a JAR, extract files from a JAR, and update an existing JAR. The document also provides details on running JAR files and using them to package and distribute Java applications and libraries.

Uploaded by

madhanrvmp7867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Ajava Unit V

The document discusses creating and working with JAR files in Java. It explains how to create a JAR file using the jar command, view the contents of a JAR, extract files from a JAR, and update an existing JAR. The document also provides details on running JAR files and using them to package and distribute Java applications and libraries.

Uploaded by

madhanrvmp7867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIT V

CREATING A JAR FILE

A JAR (Java Archive) is a package file format typically used to aggregate many
Java class files and associated metadata and resources (text, images, etc.) into
one file to distribute application software or libraries on the Java platform.

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.

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 built. The
v
verbose output tells you the name of each file as it's added to the JAR file.
0 (zero) Indicates that you don't want the JAR file to be compressed.
M Indicates that the default manifest file should not be 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 option.
m
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.
To change directories during execution of the command. See below for an
-C
example.

to create a .jar file and related commands which help us to work with .jar files
1.1 Create a JAR file
In order to create a .jar file, we can use jar cf command in the following ways as
discussed below:
Syntax:
0 seconds of 16 secondsVolume 0%

jar cf jarfilename inputfiles


Here, cf represents to create the file. For example , assuming our package pack is
available in C:\directory , to convert it into a jar file into the pack.jar , we can give
the command as:
C:\> jar cf pack.jar pack

1. 2 View a JAR file


Now, the pack.jar file is created. In order to view a JAR file ‘.jar’ files, we can use
the command as:
Syntax:
jar tf jarfilename
Here, tf represents the table view of file contents. For example, to view the
contents of our pack.jar file, we can give the command:
C:/> jar tf pack.jar
Now, the contents of pack.jar are displayed as follows:
META-INF/
META-INF/MANIFEST.MF
pack/
pack/class1.class
pack/class2.class
..
..
Here class1, class2, etc are the classes in the package pack. The first two entries
represent that there is a manifest file created and added to pack.jar. The third
entry represents the sub-directory with the name pack and the last two represent
the files name in the directory pack.

1.3 Extracting a JAR file


In order to extract the files from a .jar file, we can use the commands below
listed:
jar xf jarfilename
Here, xf represents extract files from the jar files. For example, to extract the
contents of our pack.jar file, we can write:
C:\> jar xf pack.jar
This will create the following directories in C:\
META-INF
In this directory, we can see class1.class and class2.class.
pack
1.4 Updating a JAR File
The Jar tool provides a ‘u’ option that you can use to update the contents of an
existing JAR file by modifying its manifest or by adding files. The basic command
for adding files has this format as shown below:
Syntax:
jar uf jar-file input-file(s)
Here ‘uf’ represents the updated jar file. For example, to update the contents of
our pack.jar file, we can write:
C:\>jar uf pack.jar

1.5 Running a JAR file


In order to run an application packaged as a JAR file (requires the Main-class
manifest header), the following command can be used as listed:
Syntax:
C:\>java -jar pack.jar

INTERNATIONALIZATION

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.

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:

o Messages

o Dates
o Times
o Numbers
o Currencies
o Measurements

o Phone Numbers
o Postal Addresses
o 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

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.
8. import java.util.*;
9. public class LocaleExample {
10.public static void main(String[] args) {
11.Locale locale=Locale.getDefault();
12.//Locale locale=new Locale("fr","fr");//for the specific locale
13.
14.System.out.println(locale.getDisplayCountry());
15.System.out.println(locale.getDisplayLanguage());
16.System.out.println(locale.getDisplayName());
17.System.out.println(locale.getISO3Country());
18.System.out.println(locale.getISO3Language());
19.System.out.println(locale.getLanguage());
20.System.out.println(locale.getCountry());
21.
22.}
23.}
Output:United States
English
English (United States)
USA
eng
en
US
SWING PROGRAMMING

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


.

1) AWT components are platform- Java swing components


dependent. are platform-independent.

2) AWT components are heavyweight. Swing components


are lightweight.

3) AWT doesn't support pluggable look and Swing supports pluggable


feel. look and feel.

4) AWT provides less components than Swing provides more


Swing. powerful components such
as tables, lists, scrollpanes,
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

o How to create runnable jar file in java?


o How to display image on a button in swing?
o How to change the component color by choosing a color from ColorChooser ?
o How to display the digital watch in swing tutorial ?
o How to create a notepad in swing?
o How to create puzzle game and pic puzzle game in swing ?
o 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

public void add(Component c) add a component on another component.

public void setSize(int width,int sets size of the component.


height)

public void sets the layout manager for the component.


setLayout(LayoutManager m)

public void setVisible(boolean b) sets the visibility of the component. It is by


default false.

Java Swing Examples

There are two ways to create a frame:

o By creating the object of Frame class (association)


o 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.}
ADVANCED JAVA TECHNIQUES

Advanced Java is everything that goes beyond Core Java – most importantly the
APIs defined in Java Enterprise Edition, includes Servlet programming, Web
Services, the Persistence API, etc. It is a Web & Enterprise application development
platform which basically follows client & server architecture.

Advanced Java Tutorial: Need for Advance Java


Below I have listed down few major advantages of Advance Java:

1. Advance Java i.e. JEE (Java Enterprise Edition) gives you the library to
understand the Client-Server architecture for Web Application
Development which Core Java doesn’t support.
2. J2EE is platform Independent, Java Centric environment for developing,
building & deploying Web-based applications online. It also consists of a set
of services, APIs, and protocols, which provides the functionality that is
necessary for developing multi-tiered, web-based applications.
3. You will be able to work with Web and Application Servers like Apache
Tomcat, Glassfish etc and understand the communication over HTTP
protocol. But, in Core Java, it is not possible.
4. There are a lot of Advance Java frameworks like Spring, JSF,
Struts etc. which enable you to develop a secure transaction based web
apps for the domains like E-Commerce, Banking, Legal, Financial,
Healthcare, Inventory etc.
5. To work and understand the hot technologies like Hadoop and Cloud
services, you should be prepared with core and advanced Java concepts.

Advantages of Advanced Java:


There are certain Advantages of Advanced Java are mentioned below:
1. Advanced Java has some following advantages with great quality and
capabilities, which enhance your Java application
2. Advanced Java creates Dynamic Web Applications with tools like Servlets, and
JSP (JavaServer Page).
3. It gives Scalability for your Big Java Projects with Enterprise JavaBeans (EJB),
so that smoothly your Java application can develop and scale up to handle
large workloads.
4. Having Efficient Data Management with Java Database Connectivity (JDBC).
5. It gives Reusability of Code and with such advanced frameworks like Spring,
that also promotes modular design, and makes your codebase more organised.
6. Advanced Java supports Integration of Web Services by providing Robust
Security, and Fast Development process.

Syllabus of Advanced Java:

1. Java Enterprise Edition (Java EE):


Java Enterprise Edition (Java EE), now known as Jakarta EE, it’s an remarkable
component of Advanced Java programming. It gives Scalability for Big-League
applications, that provides a smooth pattern to create applications that could
grow seamlessly as your user base expands. Basically, it’s designed to help
developers create powerful, scalable, and robust enterprise-level applications. So,
let’s delve into what Java EE brings in the context of Advanced Java.

2. Concurrency
Concurrency having the ability in Advanced Java to execute multiple tasks
simultaneously, that runs independently and concurrently by allowing different
programs. Concurrency plays an important role as it maximises the performance,
enhances responsiveness, in simpler terms concurrency implements multiple
tasks at once, it has the synchronisation that prevents the data corruption and
race conditions, and deadlocks. Concurrency is vital in modern software
development where Java provides thread-safe collections like ConcurrentMap and
ConcurrentQueue that are accessed by multiple threads concurrently.

3. Multithreading
Multithreading is the process of having several tasks running together parallelly
within the same program, it improves the responsiveness to keep users interface
responsive by saving the time while performing time-consuming tasks in the
background simultaneously that divides a large task into smaller parts, that
could run concurrently. Multithreading has Java API features that creates and
manages thread easily. In Advanced Java by using multithreading in applications
developers can create more fast, secure, responsive, and capable, so that could
handle complex tasks efficiently.

4. JDBC
JDBC stands for “Java Database Connectivity” which is a very essential
component in Advanced Java for interacting with databases. JDBC connects with
various relational databases that executes the queries, and manipulates the data
with the help of Java APIs (Application Programming Interfaces). JDBC allows the
Java program to interact with the databases to retrieve information, store the
data, and update the data. In order to overcome the problems by using JDBC
directly,Spring framework has provided one abstraction layer on top of existing
JDBC technology, we used to call this layer as Spring-JDBC. Following are the
step-by-step connection of JDBC (Java Database Connectivity):

5. Java Persistence API (JPA)


Java Persistence API is a collection of classes and methods to persist or store the
vast amount of data into a database. JPA is a specification to store or manage
Java objects or classes into the databases, which use Object-Relational Mapping
(ORM) as implementation internally to persist the database. JPA Persistence
framework needs to follow:
 Spring Data JPA: Spring Data JPA provides a higher-level abstraction layer on
the top of JPA that reduces the amount of boilerplate code needed for common
database operations.
 Spring Repository: It is a JPA specific extension for Repository, it has full API
CRUD Repository also the Paging and Sorting Repository. So basically, the JPA
repository contains APIs for basic CRUD operations, the APIs for pagination
and the APIs for Sorting.

6. Spring Framework
1. IT is a popular non-standard open-source framework developed by Interface21
INC.
2. Aims to overcome the application problems by enabling the use of simple Java
Beans(POJO-Plain Old Java Objects) to implement the business logic.
3. Enables the developers to create and test the application very easily.
4. Aims to minimise the dependency of application code on its framework.
5. Enables the use of simplicity and ease of testability in standard application.
6. The Spring Framework has a layered architecture made up of several well
defined modules.

7. Hibernate
Hibernate is a Java Persistence Framework. Persistence is the availability of the
object/data even after the process that created it,is terminated.
There are 4 layers in the hibernate architecture:
1. Java application layer
2. Hibernate framework layer
3. Backend API layer
4. Database layer

8. Java Messaging Service (JMS)


Java Messaging Service (JSM) is an important component in Advanced Java that
enables efficient communication between various software components using a
messaging paradigm. It facilitates the functionality to exchange the information
without need of direct or immediate interaction, components do not depend on
each other’s availability. JMS is built on the concept of a messaging system where
components can send and receive messages; this operates independently.

9. Java Security
The term ‘Security’ means to ensure that your application is secured, and it
checks for valid users if they login, it may login successfully if the user is
authorised, but if the user is invalid then it is denied access to the application. So
basically, it works on two scenarios i.e. Authentication, and Authorization. It is a
good practice that is designed to enhance the security of Java applications and
the Java Runtime Environment (JRE).

10. Java Design Patterns


Java Design Patterns are the reusable solutions to common problems that
frequently arise during the software designing and development phase. It provides
methods and recommendations for structuring and organising code to attain
quality and to achieve maintainability, scalability, and flexibility.

You might also like