CE 232 – OBJECT ORIENTED
PROGRAMMING WITH JAVA
YEAR 1I –SEMESTER 1I1
UNIT VI: JAVA UTILITIES
LECTURER: DR. ABDALLA H. GHARIB
WHAT IS A JAR FILE?
Java archive (jar) files are compressed files that can store one or
many files.
Jar files normally contain java or class files, but other files may
also be included.
Jar files can be run on Windows by double clicking jar files if the
JVM is installed.
WHY USE A JAR FILE?
Compression: Jar files reduce the size of the original files.
Speed: The applet can be downloaded in one http transaction.
Security: Jar files can be signed digitally. Users who recognize the
signature can optionally grant permission to run the file or
refuse.
Package sealing: Sealing a package within the jar file means that all
classes defined in that package are found in the same jar file.
WHY USE A JAR FILE?
You can use JAR files to store class files.
The class file does not target a specific hardware platform, but
instead targets the Java virtual machine architecture.
You can use JAR as a general archiving tool and also to distribute
Java programs of all types, including applets.
FLOW CHART
Create a manifest.mf file
Create a jar file
Basic jar commands
JAR subdirectories
Execute the jar file
Common mistakes
Bibliography
HOW TO CREATE A JAR FILE: SIMPLE METHOD
The jar command a utility that comes with the JDK.
The format of the basic format of the jar command is:
Jar cf jar-file input-files
HOW TO CREATE A JAR FILE
Take as an example the Java application made from the files
Foo.class
Foobar.class
To make a jar file use the command
Jar cvf Foo.jar Foo.class Foobar.class
Note that each file is separated by a space
HOW TO CREATE A JAR FILE
The execution of this command creates the file:
Foo.jar
EXECUTING JAR APPLICATION
Jar applications can be run with the following basic command:
java -jar jar-file
So to run our Foo.jar if it was an application use:
java -jar Foo.jar
EXECUTING JAR APPLICATION
Many Operating systems such as the Mac OS will allow
the execution of jar applications by simply double
clicking on them.
EXECUTING JAR APPLETS
Applets are invoked from applet tags in HTML code.
To execute an Applet simply add the name of the jar file into
the applet tag is under the archive parameter.
EXECUTING JAR APPLETS
For example, using the applet tag for your Foo.jar archive, you would use
the following:
<applet code=TicTacToe.class
archive=”Foo.jar"
width=120 height=120>
</applet>
CREATING JAR FILES: ADVANCED METHOD
The more formal and complete way to create a jar file is described in the
following slides.
Note that the more complex your process, the more difficulty a
beginning Java user is going to have with it.
CREATE MANIFEST.MF
Create a new notepad file named manifest.mf. This file contains a signal
line that points out the main class. Here is the signal line.
Main-Class:<Space>name of main class
<blank line>
Blank line must be included because some Windows OS versions need it.
For example: myClass is my main Class. So manifest.mf is following.
Main-Class: myClass
<blank line is here>
JAR FILE AND JAR COMMANDS
Next, create a jar file using the “jar” command in java. Here is the
command.
jar cvmf <the jar file name>.jar manifest.mf A.class
B.class……..
There are spaces between each file and class name.
Here are the basic jar commands
Create Jar file => jar cf jar-file input-file
View Jar file => jar tf jar-file
Extract Jar file => jar xf jar-file
JAR SUBDIRECTORIES
The JAR format also support storing files in a directory structure.
Consider the following structure.
Note: CS 602 students have had a lot of difficulty getting files to work in sub-
directories. If you have trouble, put all your files in your default Web directory.
JAR SUBDIRECTORIES
JAR SUBDIRECTORIES
If we want to keep the same structure, we can compress files
by typing:
jar cmf Sample.mf Sample.jar
Sample.class Turtle.class Sample.java
Turtle.java images
JAR SUBDIRECTORIES
The contents listing appears as:
META-INF/
META-INF/MANIFEST.MF
Sample.class
Turtle.class
Sample.java
Turtle.java
images/ images/image1.gif
images/image2.gif
images/image3.gif
EXECUTING THE JAR FILE
There are two ways to execute jar files.
1. If an OS can read *.jar as javaw.exe or java.exe, users can
double click jar files on the window. Otherwise:
2. In a command window, type in
java –jar jar-file
COMMON MISTAKES
Forgetting to leave a blank line after the main class
declaration in the manifest.mf. OS may return “Fail to
load Main-Class manifest attribute” error in Windows
machines.
Failing to compress all your files.You have to type in all
the files you want to compress. Separate these files with
a space.
TOP 10 APPLICATIONS OF JAVA
Java has become the most robust programming
language because of its amazing features.
Some of its features are platform independence, high
performance, Object orientation, support for
automatic garbage management, and many more.
In this class, we will discuss the Top 10 Applications
of Java in the real world in a detailed manner.
TOP 10 APPLICATIONS OF JAVA
Desktop GUI Applications
Mobile Applications
Artificial intelligence
Web applications
Big Data technology
Gaming applications
Business applications
Embedded systems
Cloud applications
Scientific applications
Source: Link of Top 10 Applications of Java - https://www.geeksforgeeks.org/top-10-applications-of-java-in-real-world/
DESKTOP GUI APPLICATIONS
Desktop applications can be flawlessly designed using Java. APIs
like Swing, Abstract Windowing Toolkit (AWT), and JavaFX
provide a modern way to develop GUI applications. The use of
java in desktop applications offers some fantastic benefits like
ease of learning, visual feedback, simultaneous display of multiple
instances, and many more.
MOBILE APPLICATIONS
Java is a cross-platform framework that is used to build applications that run across
smartphones and other small-screen devices. As per a survey it is observed that Java
is the second most widely used language for mobile application development. Mobile
applications that are created using Java include some popular ones like Netflix,
Twitter, Spotify, and many more.
The reason why Java is used to build mobile apps are:
Helps to write simple code
Cross-platform functioning
High compatibility with Kotlin
High security
ARTIFICIAL INTELLIGENCE
Java is one of the best languages for AI projects. Its infrastructure is well
embedded with intelligent software to enhance AI programming. It has
amazing features like better interaction with users, ease of debugging,
easy-to-code features, standard widget tools, and a lot more. The use of
Java just brings perfection to the Artificial Intelligence process.
WEB APPLICATIONS
Java is just perfect for developing web applications because of its ability to interact
with a large number of systems. It allows us to create dynamic web applications that
interact with interfaces. The presence of JSP, web servers, spring, and Hibernate
provides feasibility in the web development process. There are several advantages of
using Java for web development:
Presence of a wide range of APIs
Excellent IDEs and tools
Reusability of code
Enhanced security features
BIG DATA TECHNOLOGY
It is a software utility designed to analyze and extract information from
complex data structures. It is widely used in other technologies like deep
learning, Machine learning, and Artificial learning. Java is a viewpoint of Big
data. Java is commonly used in ETL applications like Apatar, Apache
Camel, and Apache Kafka which are used to extract complex
information. Further use of Java provides distinguished features like
automatic garbage selection and stack provision system. All these provide
it an edge over other technology and hence Java is widely used in Big
Data Technology.
INDIVIDUAL ASSIGNMENT
From the top 10 list of java application, choose one application and conduct a small
research. In your findings, write about it: what it is? Advantages, Disadvantages (if any).
Future improvements.Your personal conclusion.
Submission date: 10th January 2024 (during lecture)
Format: printed document not more than 5 pages, 12 font size, Times New Roman,
double space.
THANK YOU!
Any questions?
TUTORIAL SESSION
Java program to find the roots of a quadratic equation
TUTORIAL SESSION
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter the first coefficient: ");
Scanner sc = new Scanner(System.in);
double a = Integer.parseInt(sc.nextLine());
System.out.println("Enter the second coefficient: ");
double b = Integer.parseInt(sc.nextLine());
System.out.println("Enter the third coefficient: ");
double c = Integer.parseInt(sc.nextLine());
if(a!=0.0){
double d = (b*b)-(4*a*c);
if (d==0.0){
System.out.println("The roots are real and equal.");
double r = -b/(2*a);
TUTORIAL SESSION
System.out.println("The roots are "+ r+"and"+ r);
}else if(d>0.0){
System.out.println("The roots are real and distinct.");
double r1 = (-b+(Math.sqrt(d)))/(2*a);
double r2 = (-b-(Math.sqrt(d)))/(2*a);
System.out.println("The root1 is: "+ r1);
System.out.println("The root2 is: "+ r2);
}else{
System.out.println("The roots are imaginary.");
double rp = -b/(2*a);
double ip = Math.sqrt(-d)/(2*a);
System.out.println("The root1 is: "+ rp+ "+ i"+ip);
System.out.println("The root2 is: "+ rp+ "- i"+ip);
}
}else{
System.out.println("Not a quadratic equation.");
}
}
}
TUTORIAL SESSION
Java program to print the numbers from a given number n till 0 using recursion
TUTORIAL SESSION
class Main {
public static void main(String[] args) {
print_till_zero(8);
public static void print_till_zero(int n){
if(n==0)
return;
System.out.println(n);
n=n-1;
print_till_zero(n);
}
}
TUTORIAL SESSION
Java program to find the factorial of a number using recursion
TUTORIAL SESSION
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter an integer: ");
Scanner sc = new Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int result = fact(a);
System.out.println("The factorial of"+ a + " is: "+ result);
public static int fact(int n){
int f;
if(n==1)
f=1;
else
f = n * fact(n-1);
return f;
}
}