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

java-lab report

The document contains a series of Java Swing programming labs that demonstrate various graphical and user interface functionalities, including drawing shapes, handling user input, creating menus, and connecting to a database. Each lab includes source code examples and descriptions of the expected output. The labs cover topics such as graphics rendering, event handling, and database connectivity.

Uploaded by

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

java-lab report

The document contains a series of Java Swing programming labs that demonstrate various graphical and user interface functionalities, including drawing shapes, handling user input, creating menus, and connecting to a database. Each lab includes source code examples and descriptions of the expected output. The labs cover topics such as graphics rendering, event handling, and database connectivity.

Uploaded by

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

lOMoAR cPSD| 46745568

LAB:1 Write a Program to draw line, rectangle and ellipse using graphics 2Din
swing.

SOURCE CODE: -

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
class Shape extends JFrame {
public Shape() {
super("Rectangles Drawing Demo");
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
void drawShapes(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.drawRect(200, 200, 200, 200);
g2d.drawLine(50, 50, 200, 180);
g2d.draw(new Ellipse2D.Double(400, 400, 250, 250));
}
public void paint(Graphics g) {
super.paint(g); drawShapes(g);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Shape().setVisible(true);
lOMoAR cPSD| 46745568

} });
}}

OUTPUT: -
lOMoAR cPSD| 46745568

1
lOMoAR cPSD| 46745568

LAB: 2 Write a program to draw a rectangle and ellipse and fill


different coloron it.

SOURCE CODE: -

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
class Shape extends JFrame {
public Shape() {
super(“Rectangles Drawing Demo”);
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
void drawShapes(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Graphics2D g2 = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.fillRect(200, 200, 200, 200);
g2d.fill(new Ellipse2D.Double(400, 400, 250, 250));
}
public void
paint(Graphics g) {
super.paint(g);
drawShapes(g);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
lOMoAR cPSD| 46745568

@Override
public void run() {
new Shape().setVisible(true);
} }); }}

OUTPUT:
lOMoAR cPSD| 46745568
lOMoAR cPSD| 46745568

LAB 3: Write a program to display all font family of graphic Environment.

SOURCE CODE: -

import java.awt.GraphicsEnvironment;
public class Main {
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String familyNames[] = ge.getAvailableFontFamilyNames();
for (String familyName : familyNames) {
System.out.println("Family names: " + familyName);
}}}

OUTPUT: -

C:\Users\97798\.jdks\corretto-16.0.2\bin\java.exe "-javaagent:C:\Program
Files\JetBrains\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar=51678:C:\Program Files\JetBrains\IntelliJ
IDEA 2021.3.2\bin" - Dfile.encoding=UTF-8 -classpath
C:\Users\97798\IdeaProjects\lab1\out\production\lab1 Main

Family names: 0 Cube a Rama


DNAFamily names: 0Surendra
Family names: 11S01 Black
TuesdayFamily names: 1980
Portable
Family names: 20th Centenary
FauxFamily names: 20th Century
Font Family names: A Charming
Font
Family names: A Charming Font Expanded
Family names: A Charming Font Italic
Family names: A Charming Font
LeftleaningFamily names: A Charming
lOMoAR cPSD| 46745568

Font Outline
Family names: A Charming Font
SuperexpandedFamily names: A Cut Above
The Rest
Family names: A Yummy Apology
Family names: A.C.M.E. Secret
AgentFamily names: A750-Sans
Family names: A750-Sans-Cd-
LightProcess finished with exit
lOMoAR cPSD| 46745568

LAB 4. Write a program to draw line, rectangle, circle and ellipse using
graphics in swing .

SOURCE CODE: -

import java.awt.*;
import javax.swing.JFrame;
class Main extends Canvas{ public void paint(Graphics g) {
setBackground(Color.WHITE); setForeground(Color.BLACK);
g.drawLine(20,20,80,80);
g.drawRect(100,100,100,100);
g.drawOval(200, 200, 100, 100);
}
public static void main(String[] args) {
Main m=new Main();
JFrame f=new JFrame("Shapes in swing");
f.add(m);
f.setSize(400,400);
f.setVisible(true);
}}
OUTPUT: -
lOMoAR cPSD| 46745568

LAB 5: Write a swing program to read two input from user using input dialog
and sumthose values and display it using message dialog.

SOURCE CODE: -

import javax.swing.*;
import java.awt.*;
impo\rt java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Addition extends JFrame implements ActionListener{
JLabel label1, label2, label3;
JTextField jt1, jt2;
JButton submit;
Addition(){
setTitle("User input");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane(); c.setLayout(null);
label1 = new JLabel("Enter 1st num:");
label1.setBounds(20,50,100,20);
c.add(label1);
jt1=new JTextField();
jt1.setBounds(130,50,100,20);
c.add(jt1);
label2 = new JLabel("Enter 2nd num:");
label2.setBounds(20,100,100,20);
c.add(label2);
jt2=new JTextField();
jt2.setBounds(130,100,100,20);
c.add(jt2);
lOMoAR cPSD| 46745568

label3 = new JLabel("hello");


submit = new JButton("Calculate");
submit.setBounds(130,200,80,20);
c.add(submit);
submit.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(jt1.getText());
int b = Integer.parseInt(jt2.getText());
int c = 0;
if (e.getSource().equals(submit)) {
c = a + b;
label3.setText(String.valueOf(c));
JOptionPane.showMessageDialog(new JFrame(), label3);
}}}
class Main {
public static void main(String[] args) {
Addition a = new Addition();
}}
lOMoAR cPSD| 46745568

OUTPUT: -: -
lOMoAR cPSD| 46745568

LAB 6: Write a swing program to create Menus.

SOURCE CODE: -

import javax.swing.*;
import java.awt.event.*;
class Main {
public static JMenuBar createMenuBar()
{JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rdmi;
JCheckBoxMenuItem cbmi;
Create the menu bar.
menuBar = new JMenuBar();
//Build the File Menu.
menu = new
JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menu.getAccessibleContext().setAccessibleDescription("Dealing with Files");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("New Project...", new ImageIcon("images/newproject.png"));
menuItem.setMnemonic(KeyEvent.VK_P);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("New Project");
menu.add(menuItem);
menuItem = new JMenuItem("New File...", new
ImageIcon("images/newfile.png"));
menuItem.setMnemonic(KeyEvent.VK_F);
menu.add(menuItem);

//a group of check box menu


lOMoAR cPSD| 46745568

itemsmenu.addSeparator();
cbmi = new JCheckBoxMenuItem("A check box menu item");
cbmi.setMnemonic(KeyEvent.VK_C);
menu.add(cbmi);
cbmi = new JCheckBoxMenuItem("Another
one");cbmi.setMnemonic(KeyEvent.VK_H);
menu.add(cbmi);
//Build Edit menu in the menu bar.
menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_E);
menu.getAccessibleContext().setAccessibleDescription("Edit Menu");
menuBar.add(menu);
return menuBar;
}
public static void main(String[] args) {
final JFrame frame = new JFrame("Menu
Demo");frame.setJMenuBar(createMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
}}

OUTPUT: -
lOMoAR cPSD| 46745568

LAB 7: Write a swing program to get two number input using two text
field andmultiply it by clicking button and display it in third text field.

SOURCE CODE: -

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Mult extends JFrame implements ActionListener{
JLabel label1, label2, label3;
JTextField jt1, jt2;
JButton submit;
Mult(){
setTitle("User input");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane(); c.setLayout(null);
label1 = new JLabel("Enter First number:");
label1.setBounds(20,50,100,20); c.add(label1);
jt1=new JTextField();
jt1.setBounds(130,50,100,20);
c.add(jt1);
label2 = new JLabel("Enter Second number:");
label2.setBounds(20,100,100,20); c.add(label2);
jt2=new JTextField();
jt2.setBounds(130,100,100,20);
c.add(jt2);
label3 = new JLabel("Total is ");
label3.setBounds(20, 150, 100 ,20);
lOMoAR cPSD| 46745568

c.add(label3);
submit = new JButton("Calculate");
submit.setBounds(150,200,90,30);
c.add(submit);
submit.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(jt1.getText().isEmpty()){
JOptionPane.showMessageDialog(new JFrame(), "please enter first number");
return;
}
if(jt2.getText().isEmpty()){
JOptionPane.showMessageDialog(new JFrame(), "please enter second number");
return;
}
int a = Integer.parseInt(jt1.getText());
int b = Integer.parseInt(jt2.getText());
int c = 0;
if (e.getSource().equals(submit)) {c= a * b;
label3.setText("Total is: " + c);
}}}
class Main {
public static void main(String[] args) {
Mult m = new Mult();
}
}
lOMoAR cPSD| 46745568

OUTPUT: -
lOMoAR cPSD| 46745568

LAB 8: Write a swing program to connect database and display student details in
JTable.

SOURCE CODE: -

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet; import
java.sql.Statement;
public class DbConnector {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/tu","root","");
System.out.println("Database connected");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+"
"+rs.getString(4)+" "+rs.getString(5));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
lOMoAR cPSD| 46745568

OUTPUT: -
lOMoAR cPSD| 46745568
lOMoAR cPSD| 46745568

LAB 9: Write a database program to implementing RowSet.

SOURCE CODE: -
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbConnector {
public static void main(String[] args) {
try{

Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection
("jdbc:mysql://localhost:3306/tu","root","");
System.out.println("Database connected");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
lOMoAR cPSD| 46745568

OUTPUT: -
lOMoAR cPSD| 46745568

LAB 10: Write a swing program to implement multiple mouse event.

SOURCE CODE: -

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class Main {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public Main(){
prepareGUI();
}

public static void main(String[] args){ Main


swingListenerDemo = new Main();
swingListenerDemo.showMouseListenerDemo();
}

private void prepareGUI(){

mainFrame = new JFrame("Java SWING Examples");


mainFrame.setSize(400,400); mainFrame.setLayout(new
GridLayout(3, 1)); headerLabel = new
JLabel("",JLabel.CENTER ); statusLabel = new
JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
lOMoAR cPSD| 46745568

public void windowClosing(WindowEvent windowEvent){


System.exit(0);
}

});

controlPanel = new JPanel();


controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showMouseListenerDemo(){
headerLabel.setText("Listener in action: MouseListener");
JPanel panel = new JPanel();
panel.setBackground(Color.magenta); panel.setLayout(new FlowLayout());
panel.addMouseListener(new CustomMouseListener());
JLabel msglabel =new JLabel("Welcome to Mouse Events.",JLabel.CENTER);panel.add(msglabel);
msglabel.addMouseListener(new CustomMouseListener());
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
class CustomMouseListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
statusLabel.setText("Mouse Clicked: ("+e.getX()+", "+e.getY() +")");
}
public void mousePressed(MouseEvent e) { statusLabel.setText("Mouse
pressed: ("+e.getX()+", "+e.getY() +")");
}
lOMoAR cPSD| 46745568

public void mouseReleased(MouseEvent e) { statusLabel.setText("Mouse


released: ("+e.getX()+", "+e.getY() +")");
}

public void mouseEntered(MouseEvent e) { statusLabel.setText("Mouse


entered: ("+e.getX()+", "+e.getY() +")");
}

public void mouseExited(MouseEvent e) {


statusLabel.setText("Mouse exit: ("+e.getX()+", "+e.getY() +")");
}
}
}

OUTPUT: -
lOMoAR cPSD| 46745568

LAB 11: Write a swing program to implement layout manager.


Border layout
SOURCE CODE: -

import java.awt.*;
import javax.swing.*;
class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST JButton
b4 = new JButton("WEST");; // the button will be labeled as WEST JButton b5 =
new JButton("CENTER");; // the button will be labeled as CENTER
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}
lOMoAR cPSD| 46745568

OUTPUT: -
lOMoAR cPSD| 46745568

LAB 12: Write a swing program to check login validation by using database.

SOURCE
CODE: -
Main.java file
Package.com.logintut;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import
java.sql.DriverManager;import
java.sql.SQLException;import
java.sql.Statement;
class Myframe extends JFrame implements ActionListener{JLabel labelName,
labelEmail,labelFaculty,labelPassword; JTextField t1,t2,t3,t4;
JButton submit , Login;

private String dbURL = "jdbc:mysql://localhost:3306/validation";


private String username = "root";
private String dbpassword = "";
private Connection conn;
String tabelName = "STUDENTS";
Myframe(){
setTitle("Register");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
labelName = new JLabel("name");
labelName.setBounds(20,50,100,20);
lOMoAR cPSD| 46745568

c.add(labelName);
t1=new JTextField();
t1.setBounds(130,50,100,20);
c.add(t1);
labelEmail = new JLabel("email");
labelEmail.setBounds(20,100,100,20);
c.add(labelEmail);
t2=new JTextField();
t2.setBounds(130,100,100,20);
c.add(t2);
labelFaculty = new JLabel("faculty");
labelFaculty.setBounds(20,150,100,20)
;c.add(labelFaculty);
t3=new JTextField();
t3.setBounds(130,150,100,20);
c.add(t3);
labelPassword = new JLabel("password");
labelPassword.setBounds(20,200,100,20);
c.add(labelPassword);
t4=new JTextField();
t4.setBounds(130,200,100,20);
c.add(t4);
submit = new JButton("Submit");
submit.setBounds(150,350,80,20);
c.add(submit);
Login = new JButton("Login");
Login.setBounds(250,350,80,20);
c.add(Login);
submit.addActionListener(this);
Login.addActionListener(new ActionListener() ;
lOMoAR cPSD| 46745568

@Override
public void actionPerformed(ActionEvent e) {
SignIn lg = new SignIn();
}
});
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = t1.getText();
String email = t2.getText();
String faculty = t3.getText();
String password = t4.getText();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
boolean check_email = email.matches(emailPattern);
if(name.isEmpty()|| name.isBlank()) {
JOptionPane.showMessageDialog(new JFrame(), "Please Enter Your Name");
return;
}
if(email.isEmpty() || email.isBlank()){ JOptionPane.showMessageDialog(new
JFrame(), "Please Enter Your email");return;
}else if(!check_email){
JOptionPane.showMessageDialog(new JFrame(), "Please Enter valid email format");
return;
lOMoAR cPSD| 46745568

}
if(faculty.isBlank() || faculty.isEmpty()){ JOptionPane.showMessageDialog(new JFrame(), "Please
Enter your faculty");
return;
}
if(password.isEmpty() || password.isBlank()){
JOptionPane.showMessageDialog(new JFrame(), "Please Enter your
password");
return;
}
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dbURL,username,dbpassword);
if(conn !=null){
System.out.println("successfully connected");

}else System.out.println(" connection Failed");


String InsertQ ="INSERT INTO " + tabelName + "(name,email,faculty,password)
VALUES('"+name+"','"+email+"','"+faculty+"','"+password+"')"; Statement insertTable =
conn.createStatement(); insertTable.executeUpdate(InsertQ); System.out.println("Inserted Successfully");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch (SQLException ex){
ex.printStackTrace();
lOMoAR cPSD| 46745568

}}}
public class Main {
public static void main(String[] args){
Myframe myframe = new Myframe();
}

}
SignIn.java file
Package.com.logintut;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class SignIn extends JFrame
{JLabel labelEmail, labelPassword;
JTextField t1,t2;
JButton submit;

private String dbURL = "jdbc:mysql://localhost:3306/validation";


private String username = "root";
private String dbpassword = "";
private Connection conn;
String tabelName = "STUDENTS";
public SignIn(){
setTitle("Login");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
labelEmail = new JLabel("email");
labelEmail.setBounds(20,50,100,20);
c.add(labelEmail);
t1=new JTextField();
lOMoAR cPSD| 46745568

t1.setBounds(130,50,100,20);
c.add(t1);
labelPassword = new JLabel("password");
labelPassword.setBounds(20,100,100,20);
c.add(labelPassword);
t2=new JTextField();
t2.setBounds(130,100,100,20);
c.add(t2);
submit = new JButton("Login");
submit.setBounds(150,200,80,20);
c.add(submit);
setVisible(true);
submit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{String email = t1.getText();
String password = t2.getText();
if(email.isEmpty() ||email.isBlank()){
JOptionPane.showMessageDialog(new JFrame(), "Please Enter Your email");
return;
}
if(password.isEmpty() || password.isBlank()){
JOptionPane.showMessageDialog(new JFrame(), "Please Enter yourpassword");
return;

}
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dbURL,username,dbpassword);
if(conn !=null){
System.out.println("successfully connected");
}
else System.out.println(" connection Failed");
lOMoAR cPSD| 46745568

Statement st = conn.createStatement();
String select = "select password from students where email ='"+t1.getText()+"'";
ResultSet rs = st.executeQuery(select);
String get_password = "";
while (rs.next()){
get_password = rs.getString(1);
}
if(get_password.equals(t2.getText())){
JOptionPane.showMessageDialog(new JFrame(), "Login Success");
}else{
JOptionPane.showMessageDialog(new JFrame(), "email or password isincorrect");
}
}catch(ClassNotFoundException ex)
{ex.printStackTrace();
}
catch (SQLException ex){
ex.printStackTrace();
}}});
lOMoAR cPSD| 46745568

OUTPUT: -
lOMoAR cPSD| 46745568

LAB 13: create distributed system using RMI.

SOURCE CODE: -
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;

// Creating Remote interface for our application


public interface Hello extends Remote {
void Add(Integer a,Integer b) throws RemoteException;
}
Developing the Implementation Class (Remote Object)
ImplExample.java
// Implementing the remote interface
public class ImplExample implements Hello {
ImplExample(){super();}
// Implementing the interface method
public void Add(Integer x, Integer y) {
System.out.println("Sum = " + (x+y));
}
}
ServerRMI.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample {
public Server() {}
public static void main(String args[]) {try
{
lOMoAR cPSD| 46745568

// Instantiating the implementation class


ImplExample obj = new ImplExample();
// Exporting the object of implementation class (here we are exporting the remote objectto
the stub)
Hello skeleton = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("RMITest", skeleton);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
ClientRMI.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ClientRMI {
private ClientRMI() {}
public static void main(String[] args) {try
{
// A Java RMI registry is a simplified name service that allows clients to get a reference(a
stub) to a
remote object. In general, a registry is used (if at all) only to locate the first remote objecta
client needs to
use
lOMoAR cPSD| 46745568

// Getting the registry


Registry registry = LocateRegistry.getRegistry();
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("RMITest");
// Calling the remote method using the obtained object
stub.Add(5,10);
// System.out.println("Remote method invoked");
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}}}

OUTPUT: -
lOMoAR cPSD| 46745568

You might also like