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
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.
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.

i hv get err like file not found whn i complie my progarm hw cn i compile
what if we don’t have another drive to copy jdk.pls help.
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.
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.
T hank’s for the information that you gave….. Good luck your good work!
thanks ur way is easy to learn
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
hi
Thank u so much for the information…..actually u saved my time….once again thanks
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???
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.
hey
if this error is again coming back even setting path so just open cmd type
set path=c:\program files\java\jdk1.6\bin;c:\windows\system32;
this will solve your problem .
what’s u r problem not justified. so plz justified the problem then solve possible.
Nice Stuff!
Hi
Use following comands in CMD for ur question(windows)
set CLASSPATH=C:\Program Files\Java\jdk1.5.0\bin;
set CLASSPATH=.
Thanks
Sudheer
it means java is not working in your system …… may be java is not installed properly…
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.
solve java
very nice information
but how can we run java program with save java file in diffrent directory ?
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.
same here
You just copy the jdk folder into another drive and paste the program into bin it will work
thanks a lot yr………it is enormous stuff for me
HI Guy,
You Have really done a marvellous Job. Thanks for the information.
Yashas H Ranganath
9980784236