How to compile, save and run Java programs

How to compile, save and run Java programs

Java programs can be of two types:

1) Stand alone applications

2) Applets

Let’s discuss how to compile, save and run a Java application.

You can follow the step by step procedure to arrive at the result.

1) Create a Java program by typing its code in any text editor such 

Notepad.

2)  Save the program by the name same as that of the class containing

the main() method. For eg. if you create a program to add two

integers then name the class a as AddTwoInt

Class AddTwoInt

{

public static void main(String args[])

{
Int a=5, b=8, c=0;

c=a+b;

System.out.println(“The sum is:” + c);

}

}

Save this program in the directory where JDK is installed eg.

C:jdk1.5bin with the name AddTwoInt.java.

3) Now go to the start menu and open the “Run” dialogue box and type cmd.

You will see the command prompt.

4) Now switch to the directory where you have saved the Java program

i.e. Cjdk1.5bin

5) Type javac AddTwoInt.java to compile the program.

6) You will see that the program compiles successfully and you are left

with the prompt Cjdk1.5bin. A AddTwoInt.class file will be created in

the C:jdk1.5bin directory.

7) Now to run the program type java AddTwoInt

8) You will see the output then and there i.e. The sum is: 13

Now lets discuss how to compile, save and run a Java Applet.

1) Create a Java Applet same as the Application by typing the source

code in a text editor such as Notepad.

2) Save the program by the name same as that of the class extending

Applet class and implementing init() method. For eg. if you create an

applet to display some text and named the class as DisplayMyTextApplet

                  import java.applet.*;
                  import java.awt.*;
  
                  public class DisplayMyTextApplet extends Applet
                  {
                  public void init( )
                  { }
                  public void paint(Graphics g)
                  {
                  g.drawString(“This is my first applet!!!”,100,100);
                   }
                   }
 

Save this program in the directory where JDK is installed eg.

C:jdk1.5bin

with the name DisplayMyTextApplet.java.

3)   Now go to the “Start” menu and open the “Run” dialogue box and type

cmd. You will see the command prompt.

4) Now switch to the directory where you have saved the Java program i.e.

Cjdk1.5bin

5) Type javac DisplayMyTextApplet.java To compile the Applet.

6) You will see that the program compiles successfully and you are left with the prompt C:jdk1.5bin. A DisplayMyTextApplet.class file will be created in the C:jdk1.5bin directory.

7) Now to run the applet type appletviewer DisplayMyTextApplet. You will

notice a window displaying the desired text in the white background.

8) You can choose an alternative way to run the Java Applet i.e. to run it using the HTML page. For eg. create an HTML page with the contents

                   <html>
                   <p> This is to display my first applet                              
                   </p>                 
                   <applet code="DisplayMyTextApplet.class"      
                    height=200 width=320>
                    </applet>
                    </html>
 

9) Save the HTML file eith the name MyApplet.html , for eg., and store it in the same directory where DisplayMyTextApplet.class is stored.

10) Open the HTMl file in the browser and you will find the output.

Filed in: Automobile, Misc Tags: 

You might like:

How to add/create a new user account in Windows 8. How to add/create a new user account in Windows 8.
How to Clear App History in Windows 8 How to Clear App History in Windows 8
How to change your Password in windows 8 Operating System How to change your Password in windows 8 Operating System
How to unlock (SIM) on Samsung Galaxy Pop(mini) , Gio How to unlock (SIM) on Samsung Galaxy Pop(mini) , Gio

24 Responses to "How to compile, save and run Java programs"

  1. priti says:

    i hv get err like file not found whn i complie my progarm hw cn i compile

  2. sankalp says:

    what if we don’t have another drive to copy jdk.pls help.

  3. aliz says:

    Super job on the article. Several thanks for sharing it with us, you need to have gone via loads of work to complete it so well.

  4. Dorris Tohen says:

    APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums.

  5. cutty band says:

    T hank’s for the information that you gave….. Good luck your good work!

  6. safiya says:

    thanks ur way is easy to learn

  7. Pritesh Gajjan says:

    I’ve created dis simple program and while compiling getting following error please help me.
    Program:
    public class dog{  private int weight;  public int getweight() {  return weight;}public void setweight(int netWeight)  {   if(netWeight>0)   {     weight=netWeight;   }  }}public class TestDog{ public static void main(String[] args)  { dog d=new dog();   System.out.println(“Dog d’s weight is ” + d.getweight()); d.setweight(42); System.out.println(“Dog d’s weight is ” + d.getweight()); d.setweight(-42); System.out.println(“Dog d’s weight is ” + d.getweight());}}
    ERROR during compilation:

    class dog is public, should be declared in a file named dog.java

    I’ve saved this program as TestDog.java

  8. vajra says:

    hi
    Thank u so much for the information…..actually u saved my time….once again thanks

  9. avinash says:

    cmd shows this thing wen i compile my program….”javac is not recognised as an internal or external command,operable program or batch file”…wat does this mean???

    • Dan says:

      You havn’t set the path, you’ll need to do this before you can compile using javac:

      in cmd type – set path = c:\program files\java\jdk6.-.-\bin

      you’ll have to change that to the version you have and to the correct directory if it isn’t like that e.g. c:\java\jdk….\bin

      all should be good then but you’ll probably have to do this again each time you open cmd, or you can add it to your system class variables but #i’m too tired to explain :) hope that helps.

    • sudheer says:

      Hi
      Use following comands in CMD for ur question(windows)

      set CLASSPATH=C:\Program Files\Java\jdk1.5.0\bin;
      set CLASSPATH=.

      Thanks
      Sudheer

    • sravan says:

      it means java is not working in your system …… may be java is not installed properly…

  10. Anjali says:

    We save a java program by giving the name of the main class with .java extension. Why do we type this name in double quotes? I know it is not necessary but some people always recommend to do so. Is there any reason for this.

  11. nisarg says:

    but how can we run java program with save java file in diffrent directory ?

  12. Ankur says:

    Frnz I’m not able to save my java program in bin folder of jdk.message in the dialogue box says”You need the administraator’s permission to save “.I’m struck with my programming.Plz help.

  13. veeranarayana says:

    thanks a lot yr………it is enormous stuff for me

  14. Yashas H Ranganath says:

    HI Guy,
    You Have really done a marvellous Job. Thanks for the information.

    Yashas H Ranganath
    9980784236

Leave a Reply

Submit Comment

© 2014 Technology Blog. All rights reserved. XHTML / CSS Valid.sikiş
Proudly designed by Theme Junkie.