Java Programming For Beginners - How To Compile And Run Java Programs

Written by:  • Edited by: J. F. Amprimoz
Updated Nov 17, 2008
• Related Guides: Java | Windows | Command Prompt

In this article, we show you how to compile and run your Java program using the Command Prompt window. For convenience, we tell you how to set the "Path" variable in Windows.

Using the Command Prompt window

In this article, we show you how to compile and run your Java program. We assume that the JDK has been installed in

C:\Program Files\java\jdk1.6.0_07

and your program is stored in a file AreaOfSquare.java in the folder C:\MyJava.

We will run our programs using the “Command Prompt” window. This is also called the MS-DOS window.

Open the window by clicking on Start > All Programs > Accessories > Command Prompt

At the command prompt >, type cd C:\MyJava

The window should now look as follows (your first lines may be slightly different):

CommandPromptJava01
click to enlarge

This changes the directory to the folder containing your program, AreaOfSquare.java.

To compile the program, we would like to type

javac AreaOfSquare.java

where javac.exe is the file containing the compiler.

However, if we did this, we would get a message to the effect that javac is unknown.

The file javac.exe is found in the bin folder of the JDK.

C:\Program Files\java\jdk1.6.0_07\bin is called the “path” to the compiler. We must tell Windows where to find the compiler by setting the Path “environment variable.”

How to set the "Path"

To set Path, go to Start > Control Panel and open the System control panel.

Click on the Advanced tab and then on Environment Variables. In the section System variables, click on Path (you may need to scroll) and then on Edit. In the pop-up window, click in the Variable value field and use the right arrow key to go to the end of the field.

Type a semi-colon ( ; ) if one is not present as the last character on the line. After the semi-colon, type the path C:\Program Files\java\jdk1.6.0_07\bin\ or C:\ jdk1.6\bin\ or to wherever you installed it. Click OK all the way out to the Control Panel.

Now you can type

javac xxxx.java

(where xxxx is the name of your program) from any command prompt and Windows will know where to find the compiler.

Helpful hint: if you’ve set Path correctly and it doesn’t work, close the Command Prompt window and then reopen it. (If it is open when you set Path, it will not recognize the change.)

Compile and execute

Now that you’ve set Path, you can compile the program with

javac AreaOfSquare.java

If there are no errors in the program, the compiler will create a file called AreaOfSquare.class in the folder C:\MyJava. This file contains the Java bytecode (think of it as machine language) equivalent of the source program.

To execute the program, type

java AreaOfSquare

(Note that you do not type .class.) This invokes the Java interpreter, java.exe, also stored in the bin folder of the JDK. The Java interpreter will execute the code in the class file.

Here, the computer will type

Enter length of side:

and wait for you to enter a number. Suppose you type 12. The screen will then look like this:

Enter length of side: 12

Area of square is 144

and you are returned to the command prompt. The following shows the Command Prompt window at the end of the above activities:

CommandPromptJava02
click to enlarge


Comments

Showing all 55 comments
 
Virtual Server Hosting Jan 4, 2012 2:55 PM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
That is the basic and first part of java. Quick and easy to follow, and then simply run java program on
LuuMinhTriet Jun 15, 2011 6:46 AM
Thank to Noel Kalicharan
Thank you Noel, I had spent two days to work on compiling java from command line.
But it had not worked until i read your intruction.
Frank Apr 18, 2011 9:49 PM
Thanks So Much
I've looked through a dozen tutorials and none of them explained to me how to set up java to compile, great job explaining everything for a beginner! It works now!
Rajesh Kumar Apr 13, 2011 2:32 AM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
Thank you very much for your post.
Before i can't compile or run any java programs in my Windows 7, but now i can Compile and Run the programs. I am very happy and thanks a lot to the poster.
This website is useful and has lots of stuff.

:) :) :) :)
Lucian Mar 10, 2011 4:43 AM
Thanks!
Thank you very much for the tutorial on how to add the compiler to the system. As a noob, had no idea about that
dushyant Feb 25, 2011 11:07 AM
hi
thanks for tutorial
dushyant Feb 25, 2011 11:02 AM
thanks for tutorial
hi thanks
Gaurav Jain Feb 15, 2011 1:41 AM
thank u
thank u so much for the set path in java can we run java progm in windows 7 home premium
nasss Feb 9, 2011 10:51 PM
it didn't work
i have installed java software and set the path as given above and whenever i compile a java program it says java is not considered as internal or external command or batch file...plz help...
Abayomi Showunmi Jan 11, 2011 3:57 AM
Windows 7 not opening Java executable
I have an executable java program i want to on my Windows 7 OS pc, but the system keeps seeing it as Jar file. If i return it to Windows XP, the Xp system sees it as Jar.exe.
Pls what can i do???
Noel Kalicharan Dec 23, 2010 8:56 PM
Response to mark
Yes, the JDK contains the compiler. See article 5 above.
mark Dec 23, 2010 6:41 PM
jdk
when compiling a java program , is jdk is a requirement to be installed at my pc??
Noel Kalicharan Dec 17, 2010 8:17 PM
Response to Mooper
The link from the previous article

http://java.sun.com/javase/downloads/index.jsp

still works. It is redirected to the oracle page. I tried it and it works fine.

Please try again. If it doesn't work there may be a problem with your computer.
Mooper Meepa Dec 17, 2010 5:11 PM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
What a great site, full of useful information.

It's a pity I cannot download the JDK from Sun now known as oracle.
Something to do with a failed Cab file.
Still They may have fixed it by 2059. it's been broken for since January 2009.

Pity Java could be used by more people, this will put millions off it forever.
Noel Kalicharan Nov 10, 2010 5:29 PM
To Anjali
You're very welcome. I'm glad it helped.
Anjali Nov 10, 2010 1:45 PM
anjali
Thanks for the tutorial....
i Nov 9, 2010 10:20 AM
hi
iiiiiiiiiiiiii
Pratap Nov 8, 2010 12:46 PM
Other methods to execute
Sir, can you tell any other complier instead of jdk1.6 to execute my .java files directly? Where command prompt is not necessary ...like a browser for HTML?

Thank you
Sumedh Lakade Oct 25, 2010 2:53 PM
it didn't work
I tried these steps on my windows 7 quite a few times but didn't work
Os: Windows 7
Java Version. 5
Java path: C:\java1\jdk1.5.0_05

Tell me the solution.... Plz Reply
thanks
george Oct 12, 2010 2:17 PM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
thanks this tutorial helped me.
Noel Kalicharan Oct 4, 2010 3:55 PM
javac not recognised
If you are getting "javac not recognised" it means that the path has not been set properly. Remember that setting the path is the way to tell the computer where to find 'javac'.
laxman Oct 4, 2010 3:10 PM
java error
in my program files\java folder..
after setting my path i m getting error like
'javac' is not recognised as a internal or external command..operable program or batch file.
pls help..
i hv to do my project on java..
Noel Kalicharan Sep 18, 2010 2:13 PM
To HDM
Thanks for letting me know. It's always nice to hear that someone has found the article useful.
HDM Sep 18, 2010 2:03 PM
THANKS!
i've been trying forever to get this to work and this article helped more than anything else!!! thank you sir!
Bhola Sep 9, 2010 7:22 AM
compile error
sir while iam trying to compile my java program iam facing an error .
welcome.java:can not read.



plese sir help me
Rahul Sep 7, 2010 11:30 AM
java programming
how can i dibbug java program & how can give the path on commnd prompt
Noel Kalicharan Aug 31, 2010 11:46 AM
Respose to Iaavanya
File not found: there are two things to check (1) make sure that you have spelt the file name correctly (2) make sure that the command prompt is showing the folder which contains your file (re-read the article to see what to expect). For example if your file is in the folder c:\myjava then the command prompt should read >c:\myjava.
laavanya Aug 31, 2010 4:34 AM
hello sir
while iam trying to compile my java program iam facing an error
javac : file not found
usage:javac <options> <source files>


suggest me the solution for this .....im using jdk1.6.....windowsxp
rohit..mumbai Aug 25, 2010 11:18 AM
compile program
sir,,
i have jre1.6 in my program files\java folder..
after setting my path i m getting error like
'javac' is not recognised as a internal or external command..operable program or batch file.
pls help..
i hv to do my project on java..
plz
divyang Aug 7, 2010 1:09 PM
compilation error
when i compile my java prgs that should doesn't run prg and not find path i use java 6 so help me
Dipen Jul 31, 2010 5:44 AM
compil error
when i insert java program's path in cmd that time "canot read this file" error comes. so pls tell m why it comes?????
karunakar Jul 30, 2010 4:26 AM
problem at run time
good afternoon

am insalling jdk1.6.0update21 in my system and set the path and class path, but compilisation is working but .class file is not executing its giving some errors no defination found for class/classname. its working some system and not working some systems

please send the solution
Cathal Jul 28, 2010 12:38 PM
having problems
I am using the jdk.1.6.0_20 update.
I have windows 7 and I have set rthe enviroment variables as C:\Program Files (x86)\Java\jdk1.6.0_20\bin\

in command Promt i get as far as C:\MyJava>
When i type javac AreaOfSquare.java I get
javac: not a file: AreaOfSquare.java
Useage: javac <options> <source files>

Thank You for what ever help yopu can give me.
chintu Jul 18, 2010 1:35 AM
Thanks.........
Sir i have done everything you said and java programmes are now successfully compiling as well as running ....................thank you very much
Swathi Jun 18, 2010 6:26 AM
compile error
sir,
i hav done as u said the above procedure but after writing the program in notepad as xxx.java in desktop .

At the time of compiling at cmd , i got in to desktop folder and compiled as " javac xxx.java "

iam getting as " javac is not recnised as internal or external file ."

sir i need ur advice in running program in my system.
sanjay Jun 10, 2010 3:42 AM
no system variable named path.
hello sir,
there is no path given in the system variables.i even tried creating the path variable but to no avail.
ReBaZ_BMW Jun 2, 2010 6:39 AM
Thank you Master...
Thank you Master ....
Befor read your Writting I cann't Run my prog.
But now I can Run it.
Thank you
rajani kant May 30, 2010 1:57 AM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
how 2 make path in java in windows 7...sir i have j2re1.4.2_18 version.....plz help me
Payal May 8, 2010 3:31 AM
how 2 compile & run java prog
I have d j2sdk1.4.2_03 version ,but while compiling my prog I'm getting error" error 1:cannot read :progname.java" Sir plz help me.How 2 resolve this error
priyanka Apr 22, 2010 1:25 AM
compile error
is above given path is also suitable for java jre6 ?
veeranna Apr 21, 2010 6:49 AM
Error
symbol cannot find symbol
vaishali Apr 19, 2010 2:29 AM
how to compile java program
Sir,
while compiling java sourcecode im getting error like "error can not read java"i have installed 1.4.2_01 version.then how i can overcome this error
Noel Kalicharan Apr 6, 2010 10:17 AM
Response to Shruti
Please see my reponse to Jahar below. The post by "parne" might also be useful. If you are sure you have set the path correctly, and assuming your program is stored in the folder "MyJava" on your C drive, try typing

javac C:\MyJava\selectionsort.java

Hope this helps.

Noel Kalicharan
shruthi Apr 6, 2010 2:37 AM
probelm in compiling the java program
sir i have done all yous steps to compile java program.but i can set the path..after that while typing javac selectionsort.java ,it shows
javac:file not found:selectionsort.java
usage:javac<options><source files>
use-help for a list of possible options
sir..i have saved my file by class name.java,
whats the problem sir
Veeranna Feb 17, 2010 7:40 AM
How to compile program having package
Sir, How to compile program having package in command prompt?
Noel Kalicharan Feb 4, 2010 9:34 AM
Reply to saranya
I use this method to run any kind of Java program, including those with GUI elements. If you are getting an error, most likely there is an error in your program. If it helps, try running the following:

import javax.swing.JOptionPane;

public class HelloWorldGUI {

public static void main(String[] args) {
JOptionPane.showMessageDialog( null, "Hello World!");
}

}

saranya Feb 4, 2010 7:24 AM
sir
one qustion.when i have program in creating
GUI how to run the program.
when i compile normal method it shows error.
parne Jan 16, 2010 5:41 AM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
well u can do this in other way also
in cmd prompt goto the dir where
u saved ur file there type set path=%path%;<path of javac.exefolder>
eg: set path=%path%;C:\Program Files\Java\jdk1.6.0\bin

and btw thankz i didnot know that before
Noel Kalicharan Dec 6, 2009 10:00 PM
Response to jahar
Please check the following:

1. Make sure that your command prompt shows the folder which contains the file selectionsort.java. For example, if the program is in the folder MyJava on the C: drive, make sure the prompt is showing

C:\MyJava>javac selectionsort.java

2. Make sure that the file name is spelt exactly as in your javac command (upper and lowercase matters).

3. If you had opened the command prompt window BEFORE setting the path, close the window and then reopen it. (If it was already open when you set the path, it would not recognize the change.)

If none of these works, double check that you have set the path correctly.

Noel Kalicharan
jahar Dec 6, 2009 12:04 PM
How to compile..
sir i have done all yous steps to compile java program.but i can set the path..after that while typing javac selectionsort.java ,it shows
javac:file not found:selectionsort.java
usage:javac<options><source files>
use-help for a list of possible options
sir..i have saved my file by class name.java,
whats the problem sir...
michael dobson Oct 22, 2009 9:37 AM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
excellent article. it help very much. im very happy now lol :D
michael Oct 22, 2009 8:13 AM
RE: Java Programming For Beginners - How To Compile And Run Java Programs
well done, very good
http://whatisosireferencemodel.blogspot.com/
linette raju Sep 6, 2009 3:32 AM
Thank you sir
Your blog has definitely helped in clarifying my doubt.Its a really good article .Pls continue the good work

zeray Jul 24, 2009 4:49 AM
How To Compile And Run Java Programs
I am really grateful for your clear and easy to to understand article

keep it up!!
Jaymin Jul 23, 2009 4:12 PM
Thaks..
Thanks.... Sir i can solve easily my query perfectly...
 
blog comments powered by Disqus
Email to a friend