Home About Me C Programs C++ Programs Make A Blog Popular

How To Compile and Run a JAVA Program :

Monday, July 9, 2007

Step 1: Type your JAVA Program into any text editor like notepad or Edit (Dos Based Editor).

Step 2: Save your program (filename.java). You should use four characters extension .java just for example

hello.java.

Step 3: Compile your program with the follwing command at dos prompt
where your java compiler exist. in my case c:\jdk folder.

So first of all you have to change the directory

c:\> cd jdk
c:\jdk> cd bin

c:\jdk\bin> JAVAC hello.java

Javac is a java compiler.
if compilation is successfully done, it will create a hello.class file.

If your program contains 3 classes then it automatically creates 3 .class file, each file for each classes.
It is the file which contains byte code.

To run a java program

c:\jdk\bin>java hello

where java is a interpreter and hello is a .class file. And you are done. you have sucessfully compiled and run

your java program.

4 comments:

lyndzxnacole0o7 said...

How do i get jre or jdk to work on vista?

sindhu said...

an error has appeared during compilation as cannot read example.java where example.java is the name i have saved the program in notepad.......
may i know how to handle this error

Nitin Hindustani said...

Hi, Sindhu thanx for commenting in my blog. Please tell me where you are saving example.java and how are you compiling the program tell me in deatail then only I will show you the process.

rrr said...

Hi nitin even I'm facing the same problem.I have saved my source file in folder c:\>java
however I can compile the file If I save it the bin folder of the J2sdk.
Is it necessry to always save in the bin folder of the j2sdk to compile??
wat if files are placed in a seperate folder than how to compile??