Honhar Engineer

Honhar Engineer
Thanks for visiting,stay connected for more updates !
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, September 26, 2013

Definition of RMI & RPC,Difference between RPC & RMI,Codes for RPC & RMI

Remote Procedure Call (RPC)
Remote Procedure Call (RPC) is a powerful technique for constructing distributed client-server based applications.

RPC is a client /server infrastructure that increases the interoperability, portability and flexibility of an application by allowing the application to be distributed over multiple heterogeneous platforms.

An RPC is analogous to function call.

How RPC Works

An RPC is analogous to a function call. Like a function call, when an RPC is made, the calling arguments are passed to the remote procedure and the caller waits for a response to be returned from the remote procedure. Figure 32.1 shows the flow of activity that takes place during an RPC call between two networked systems. The client makes a procedure call that sends a request to the server and waits. The thread is blocked from processing until either a reply is received, or it times out. When the request arrives, the server calls a dispatch routine that performs the requested service, and sends the reply to the client. After the RPC call is completed, the client program continues. RPC specifically supports network applications.

Fig. 32.1 Remote Procedure Calling Mechanism A remote procedure is uniquely identified by the triple: (program number, version number, procedure number) The program number identifies a group of related remote procedures, each of which has a unique procedure number. A program may consist of one or more versions. Each version consists of a collection of procedures which are available to be called remotely. Version numbers enable multiple versions of an RPC protocol to be available simultaneously. Each version contains a a number of procedures that can be called remotely. Each procedure has a procedure number.

RPC Application Development

Consider an example:

A client/server lookup in a personal database on a remote machine. Assuming that we cannot access the database from the local machine (via NFS).

We use UNIX to run a remote shell and execute the command this way. There are some problems with this method:

* the command may be slow to execute.
* You require an login account on the remote machine.

The RPC alternative is to

* establish an server on the remote machine that can repond to queries.
* Retrieve information by calling a query which will be quicker than previous approach.

To develop an RPC application the following steps are needed:

* Specify the protocol for client server communication
* Develop the client program
* Develop the server program

The programs will be compiled seperately. The communication protocol is achieved by generated stubs and these stubs and rpc (and other libraries) will need to be linked in.

Reomote Procedure Calling Mechanism

A remote procedure is uniquely identified by the triple: ( program number, version number, procedure number).

ADVANTAGES:

Many distributed systems use Remote Procedure Calls(RPCs) as their main communication mechanism.

RMI is a Remote method invocation


- RMI (Remote Method Invocation) is a way that a programmer, using the Java programming language and development environment, can write object-oriented programming in which objects on different computers can interact in a distributed network. RMI is the Java version of what is generally known as a remote procedure call (RPC), but with the ability to pass one or more objects along with the request. The object can include information that will change the service that is performed in the remote computer. Sun Microsystems, the inventors of Java, calls this "moving behavior." For example, when a user at a remote computer fills out an expense account, the Java program interacting with the user could communicate, using RMI, with a Java program in another computer that always had the latest policy about expense reporting. In reply, that program would send back an object and associated method information that would enable the remote computer program to screen the user's expense account data in a way that was consistent with the latest policy. The user and the company both would save time by catching mistakes early. Whenever the company policy changed, it would require a change to a program in only one computer.

Sun calls its object parameter-passing mechanism object serialization. An RMI request is a request to invoke the method of a remote object. The request has the same syntax as a request to invoke an object method in the same (local) computer. In general, RMI is designed to preserve the object model and its advantages across a network.

RMI is implemented as three layers:

* A stub program in the client side of the client/server relationship, and a corresponding skeleton at the server end. The stub appears to the calling program to be the program being called for a service. (Sun uses the term proxy as a synonym for stub.)
* A Remote Reference Layer that can behave differently depending on the parameters passed by the calling program. For example, this layer can determine whether the request is to call a single remote service or multiple remote programs as in a multicast.
* A Transport Connection Layer, which sets up and manages the request.

A single request travels down through the layers on one computer and up through the layers at the other end.



Difference between RMI and RPC?


RMI or Remote Method Invokation is very similar to RPC or Remote Proceedure call in that the client both send proxy objects (or stubs) to the server however the subtle difference is that client side RPC invokes FUNCTIONS through the proxy function and RMI invokes METHODS through the proxy function. RMI is considered slightly superior as it is an object-oriented version of RPC.




Application Difference Between RPC and RMI?


The only real difference between RPC and RMI is that there is objects involved in RMI: instead of invoking functions through a proxy function, we invoke methods through a proxy.

What this means in practice is that we now want the client to hold references to remote objects that it can invoke methods on. These references should behave just like local objects, but when invoked dispatch the method invocation to the remote object.

Because we need to refer to both an object and a method now - not just a function - we extend the protocol so it first sends an object id across the socket, then the method and then the arguments. The server then dispatches the method to the remote object based on id and method name.-Rituparno Ganguly


Name :- SANJAY KUMAR JHA
Assignment name :- Remote Procedure Call (RPC)

// SERVER

import java.io.*;
import java.net.*;

class ClientHandler extends Thread {
protected Socket incoming;

public ClientHandler(Socket incoming) {
this.incoming = incoming;
}
public void run() {
int x,y;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
PrintWriter out=null ;// = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));
out=new PrintWriter(incoming.getOutputStream(),true);
String str = in.readLine();
if (str.equals("add"))
{
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
int z=add(x,y);
out.flush();
out.println(z);
}
else if(str.equals("sub"))
{
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
int z=sub(x,y);
String s1="" + z;
out.println(s1);
}
incoming.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}

public int add(int x1,int y1)
{
return x1+y1;
}
public int sub(int x1,int y1)
{
return x1-y1;
}

}

public class Server {
public static void main(String[] args) {
System.out.println("Server started.");
try {
ServerSocket s = new ServerSocket(9090);
for (;;) {
Socket incoming = s.accept();
new ClientHandler(incoming).start();
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}




// CLIENT



import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;

@SuppressWarnings("serial")
public class Client extends Applet implements ActionListener
{
Button ADD,SUB;
TextField t1,t2,t3;
public void init()
{
ADD=new Button("ADD");
SUB=new Button("SUB");
t1=new TextField(10);
t2=new TextField(10);
t1.setBounds(430,00,70,20);
t2.setBounds(500, 00, 70, 20);
ADD.setBounds(430, 50, 70, 20);
SUB.setBounds(500, 50, 70, 20);
ADD.addActionListener(this);
SUB.addActionListener(this);
t3=new TextField(10);
t3.setBounds(500, 100, 70, 20);
add(t3);
add(t1);
add(t2);
add(ADD);
add(SUB);
}
public void start() {
}
public void actionPerformed(ActionEvent ae)
{
String x1;
String s=ae.getActionCommand();
String p1,p2;
if(s.equals("ADD"))
{
BufferedReader in=null;
PrintWriter out=null;
Socket t=null;
String line1="add";
try {
String host="localhost";
t = new Socket(host, 9090);
in=new BufferedReader(new InputStreamReader(t.getInputStream()));
out=new PrintWriter(t.getOutputStream(),true);
out.println(line1);
p1=t1.getText();
p2=t2.getText();
System.out.println("x= "+p1+ "y= "+p2);
out.println(p1);
out.println(p2);
System.out.println("add1");
x1=in.readLine();
System.out.println("Addition of " + p1 + "and " + p2 + " : " + x1);
t3.setText(x1);
} catch (Exception e) {System.out.println("Error: " + e); }
System.out.println("Addition Performed");
}
else if(s.equals("SUB"))
{
BufferedReader in=null;
PrintWriter out=null;
Socket t=null;
String line1="sub";
try {
String host="localhost";
t = new Socket(host, 9090);
in=new BufferedReader(new InputStreamReader(t.getInputStream()));
out=new PrintWriter(t.getOutputStream(),true);
out.println(line1);
p1=t1.getText();
p2=t2.getText();
out.println(p1);
out.println(p2);
x1=in.readLine();
System.out.println("Subtraction of " + p1 + "and " + p2 + " : " + x1);
t3.setText(x1);
} catch (Exception e) { System.out.println("Error: " + e); }
System.out.println("Subtraction Performed");
}
}
}


// RMI

//SERVER


import java.net.*;
import java.rmi.*;

public class server
{
public static void main(String args[])
{
try
{
serverImpl serverImpl=new serverImpl();
Naming.rebind("Server",serverImpl);

}
catch (Exception ex)
{
System.out.println ("Exception .."+ex);
}
}
}

//SERVERIMPL

import java.rmi.*;
import java.rmi.server.*;
public class serverImpl extends UnicastRemoteObject implements

Serverintf
{

public serverImpl()throws RemoteException
{}
public double add(double d1,double d2)throws RemoteException
{
return d1+d2;
}
public double sub(double d1,double d2)throws RemoteException
{
return d1-d2;
}
public double mm(double d1,double d2)throws RemoteException
{
return d1*d2;
}
public double dd(double d1,double d2)throws RemoteException
{
return d1/d2;
}
}

// SERVER INTF

import java.rmi.*;

public interface Serverintf extends Remote
{
double add(double d1,double d2)throws RemoteException;
double sub(double d1,double d2)throws RemoteException;
double mm(double d1,double d2)throws RemoteException;
double dd(double d1,double d2)throws RemoteException;

}

// CALCULATOR

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;

class calculator extends JFrame implements ActionListener
{
String op,o1,o2;
JButton

b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b;
JTextField r;
JPanel p;
calculator()
{
p=(JPanel)getContentPane();
r=new JTextField(15);
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b10=new JButton("0");

b11=new JButton("+");
b12=new JButton("-");
b13=new JButton("*");
b14=new JButton("/");
b15=new JButton(".");
b16=new JButton("=");
b17=new JButton("Clear");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);

b1.setActionCommand("1");
b2.setActionCommand("2");
b3.setActionCommand("3");
b4.setActionCommand("4");
b5.setActionCommand("5");
b6.setActionCommand("6");
b7.setActionCommand("7");
b8.setActionCommand("8");
b9.setActionCommand("9");
b10.setActionCommand("0");
b11.setActionCommand("+");
b12.setActionCommand("-");
b13.setActionCommand("*");
b14.setActionCommand("/");
b15.setActionCommand(".");
b16.setActionCommand("=");
b17.setActionCommand("Clear");

p.setLayout(new FlowLayout());
p.add(r);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
p.add(b17);

addWindowListener(new WEvent());

}
class WEvent extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
String s,s1;
s=e.getActionCommand();
if (s.equals("="))
{

try
{
String

ServerURL="rmi://127.0.0.1/Server";
// String ServerURL="rmi://192.168.100.10/Server";
Serverintf

serverintf=(Serverintf)Naming.lookup(ServerURL);
o2=r.getText();
double

d1=Double.valueOf(o1).doubleValue();
double

d2=Double.valueOf(o2).doubleValue();

double res=0;

if(op.equals("+"))
{
res=serverintf.add(d1,d2);
}

else if(op.equals("-"))
{
res=serverintf.sub(d1,d2);
}
else if(op.equals("*"))
{
res=serverintf.mm(d1,d2);
}
else if(op.equals("/"))
{
res=serverintf.dd(d1,d2);
}

String result=""+res;
r.setText(result);

}
catch(Exception ee)
{
System.out.println("Exception"+ee);
}
}
else

if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))
{
op=s;
o1=r.getText();
r.setText("");
}

else if(s.equals("Clear"))
{
op="";
o1="";
o2="";
r.setText("");
}
else
{
s1=r.getText();
r.setText(s1+s);
}
}

public static void main(String args[])
{
calculator c;
c=new calculator();
c.setSize(200,250);
c.setTitle("CALCULATOR");
c.setVisible(true);

}
}

Monday, August 12, 2013

Java program for understanding Mouse Listeners

import java.awt.EventQueue;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseMotionListener;

public class MouseLabel {

    JLabel mouseLabel;
    JLabel mouseMoveLabel;
    JTextArea mouseEvents;
   
    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
    
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
         {
            
            @Override
             public void run()
             {
                
                 new MouseLabel();        
             }
         });
             
    }
   
    public MouseLabel()
    {
        JFrame guiFrame = new JFrame();
       
        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Mouse Event Example");
        guiFrame.setSize(700,300);
     
        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
       
        //creating a border to highlight the label areas
        Border outline = BorderFactory.createLineBorder(Color.black);
       
        //mouseLabel will trigger the Mouse click evetns
        mouseLabel = new JLabel("Interactive Label", JLabel.CENTER);
        mouseLabel.setBorder(outline);
       
        //Attach the MouseListener to mouseLabel
        //as an anonymous inner class.
        //Each method changes the test of mouseLabel and
        //logs the mouse event in the JTextArea
        mouseLabel.addMouseListener(new MouseListener()
        {
           
            @Override
            public void mouseClicked(MouseEvent e)
            {
               
                mouseLabel.setText("I've been clicked!");
                mouseEvents.append("MouseClicked Event");
                mouseEvents.append(e.getClickCount() + " click(s)\n");
                mouseEvents.append("Xpos: " + e.getX() + " Ypos: " + e.getY() + "\n");
               
            }
            @Override
            public void mousePressed(MouseEvent e)
            {
               
                mouseLabel.setText("You're holding the mouse button aren't you?");
                mouseEvents.append("MousePressed Event\n");
            }
           
            @Override
            public void mouseExited(MouseEvent e)
            {
                mouseLabel.setText("The mouse has run away!");
                mouseEvents.append("MouseExited Event\n");
            }
           
            @Override
            public void mouseEntered(MouseEvent e)
            {
               
                mouseLabel.setText("I can feel the presence of the Mouse");
                mouseEvents.append("MouseEntered Event\n");
            }
           
            @Override
            public void mouseReleased(MouseEvent e)
            {
               
                mouseLabel.setText("You've let go of the mouse button");
                mouseEvents.append("MouseReleased Event\n");
            }
           
        });
       
        //mouseMoveLabel triggers the mouse motion events
        mouseMoveLabel = new JLabel("Drag the Mouse!");
        mouseMoveLabel.setBorder(outline);
       
        //Attach the MouseMotionListener to mouseMoveLabel
        //as an anonymous inner class.
        //Each method logs the events triggered in the JTextArea
        mouseMoveLabel.addMouseMotionListener(new MouseMotionListener(){
            @Override
            public void mouseDragged(MouseEvent e)
            {          
                mouseEvents.append("MouseDragged Event\n");
            }

            @Override
            public void mouseMoved(MouseEvent e)
            {
                mouseEvents.append("MouseMoved Event\n");
               
            }
       
        });
       
       
        mouseEvents = new JTextArea("The Mouse events can be seen here:\n");
       
        //Place the JTextArea into a JScrollPane to be able to scroll
        //through all the events logged. It's also perfect for listening
        //for MouseWheelListener events
        JScrollPane textScroll = new JScrollPane(mouseEvents);
       
        //Attach the MouseWheelListener to textScroll
        //as an anonymous inner class.
        //As some many events are fired by the mouse moving the method
        //just changes the text of mouseLabel rather than logging them
        //all in the JTextArea.
        textScroll.addMouseWheelListener(new MouseWheelListener(){
       
            @Override
            public void mouseWheelMoved(MouseWheelEvent e)
            {
                mouseLabel.setText("You've moved the mouse wheel");
            }
       
        });
       
        guiFrame.add(mouseMoveLabel, BorderLayout.WEST);
        guiFrame.add(mouseLabel, BorderLayout.CENTER);
        guiFrame.add(textScroll, BorderLayout.EAST);
        guiFrame.setVisible(true);
    }
    }

Sunday, August 11, 2013

Get IP Address using Java code

import java.util.*;
import java.lang.*;
import java.net.*;  
public class GetOwnIP
{
public static void main(String args[])
{
try
{
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+ownIP.getHostAddress());
}
catch (Exception e)
{
System.out.println("Exception caught ="+e.getMessage());
}
}
}

Thursday, March 28, 2013

Why Java doesn't support multiple inheritance ?

1) First reason is ambiguity around Diamond problem, consider a class A has foo() method and then B and C derived from A and has there own foo() implementation and now class D derive from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see below
           A foo()
           / \
          /   \
B foo()      C foo()
          \   /
           \ /
            D
           foo()


In my opinion even if we remove the top head of diamond class A and allow multiple inheritances we will see this problem of ambiguity.

Some times if you give this reason to interviewer he asks if C++ can support multiple inheritance than why not Java. hmmmmm in that case I would try to explain him the second reason which I have given below that its not because of technical difficulty but more to maintainable and clearer design was driving factor though this can only be confirmed by any of java designer and we can just speculate. Wikipedia link has some good explanation on how different language address problem arises due to diamond problem while using multiple inheritances.

2) Second and more convincing reason to me is that multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc and given that there are not many scenario on which you need multiple inheritance its wise decision to omit it for the sake of simplicity. Also java avoids this ambiguity by supporting single inheritance with interfaces. Since interface only have method declaration and doesn't provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.

First Java Program

Looking at First Java program :

This is demonstrating simple example program that displays the text,” My First JAVA program...............” on the console.
 public class FirstProg
{ //This is a first simple java program.
 public static void main(String[] args)
 {
  System.out.println("My First JAVA program...............");
 }
}

Explanation :

public :
A keyword of the Java language that indicates that the element that follows should be made available to other Java elements.
As a result, this keyword indicates that the FirstProg class is a public class, which means other classes can use it.

class :
Another Java keyword that indicates that the element being defined here is a class. All Java programs are made up of one or more classes.
A class definition contains code that defines the behavior of the objects created and used by the program.

FirstProg :
An identifier that provides the name for the class being defined here. While keywords, such as public and class, are words that are defined by the Java programming language, identifiers are words that you create to provide names for various elements you use in your program.
In this case, the identifier FirstProg provides a name for the public class being defined here.
{ :
The opening brace on line 2 marks the beginning of the body of the class. The end of the body is marked by the closing brace on line 7.
Everything that appears within these braces belongs to the class. As you work with Java, you’ll find that it uses these braces a lot.

//This is a first simple java program. :
This is a comment. Like most other programming languages, Java lets you enter a remark into a program’s source file.
The contents of a comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code.
In this case, the comment describes the program and reminds you that the source file should be called FirstProg.java. Of course, in real applications, comments generally explain how some part of the program works or what a specific feature does.
Java supports three styles of comments. The one shown at this program is called a single-line comment. This type of comment must begin with // as shown in the program.
The second style of the comment is multiline comment. This type of comment must begin with /* and end with */. Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiline comment may be several lines long.
For example :
          /*This is a first
          simple java program. */
The third style of the comment is documentation comment. This type of comment is used to produce an HTML file that documents your program.
The documentation comment begins with a /** and ends with a */. Documentation comments are explained in next chapter.

public :
The public keyword is used again, this time to indicate that a method being declared here should have public access.
That means classes other than the FirstProg class can use it. All Java programs must have at least one class that declares a public method named main.
The main method contains the statements that are executed when you run the program.

static :
If you are want to execute any elements (properties) without an object means before the object creating then it must declare a static so compiler directly execute those static elements.
Here the main method is executed before the any object is creating so it must declare a static. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class.
This is necessary since main( ) is called by the Java interpreter before any objects are made.

void :
In Java, a method is a unit of code that can calculate and return a value.
For example :
you could create a method that adds two numbers. Then, the addition would be the return value of the method.
If a method doesn’t need to return a value, you must use the void keyword to indicate that no value is returned. Because Java requires that the main method not return a value, you must specify void when you declare the main method.

main :
Finally, the identifier that provides the name for this method.
Java requires that this method be named main because main( ) is the method called when a Java application begins.
Besides the main method, you can also create additional methods with whatever names you want to use.

(String[] args) :
It’s called a parameter list, and it’s used to pass data to a method.
Java requires that the main method must receive a single parameter that’s an array of String objects.
By convention, this parameter is named args. If you don’t know what a parameter, a String, or an array is, don’t worry about it You can find out what a String, parameters and arrays are in the next chapter.
You have to write (String[] args) on the declaration for the main methods in all your programs. In this case, args receives any command-line arguments present when the program is executed.
This program does not make use of this information, but several other programs use command-line argument.

Another { :
Another set of braces begins at line 4 and ends at line 6. These mark the body of the main method.
Notice that the closing brace in line 6 is paired with the opening brace in line 4, while the closing brace in line 7 is paired with the one in line 2.
This type of pairing is commonplace in Java. In short, whenever you come to a closing brace, it is paired with the most recent opening brace that hasn’t already been closed — that is, that hasn’t already been paired with a closing brace.

 System.out.println(“My First JAVA program...............”); :
This is the only statement in the entire program. It calls a method named println that belongs to the System.out object.
System is a predefined class, it is automatically included in your programs that provides access to the system and out is the output stream that is connected to the console.
The println method displays a line of text on the console. The text to be displayed is passed to the println method as a parameter in parentheses following the word println.
In this case, the text is the string literal My First JAVA program............... enclosed in a set of double quotation marks. As a result, this statement displays the text My First JAVA program............... on the console.

Note: In Java, statements end with a semicolon. Because this is the only statement in the program, this line is the only one that requires a semicolon.
Java is case-sensitive. Thus, Main is different from main.
} :
Line 6 contains the closing brace that marks the end of the main method body that was begun by the brace on line 4.
Another } :
Line 7 contains the closing brace that marks the end of the FirstProg class body that was begun by the brace on line 2. Because this program consists of just one class, this line also marks the end of the program.

To run this program, first of all save a text file named FirstProg.java . Then, compile it by running this command at a command prompt:

javac FirstProg.java
This command creates a class file named FirstProg.class that contains
the Java bytecodes compiled for the FirstProg class.
Now run the program by entering this command:
java FirstProg
Output :
My First JAVA program...............