JAVA Beginners part 2


The CLASSPATH environment variable is used by Java to determine where to look for classes referenced by a program. If, for example, you have an import statement for my.package.Foo, the compiler and JVM need to know where to find the my/package/Foo class.

What is the purpose of these variables ?

The JAVA_HOME variable is used by other programs to know where your JDK is. Most IDEs will use it. It is also used as a convenient shortcut, to avoid having to change the other places referring to the JDK install directory. For example, we have used it to set the PATH variable (%JAVA_HOME%). This is very convenient, because if you install another version of the JDK, all you need to do is to update your JAVA_HOME, without having to touch the PATH.

Running my first JAVA program.

If you have not installed JAVA before, then goto Oracle Website and download JAVA JDK.
Note, JAVA JDK is essential to compile JAVA code. Lot of Systems have JAVA JRE pre installed which is good enough to run the JAVA programs but it does not have the capability to compile JAVA programs. So you need JDK. These days JRE comes along with the JDK package. So all you need to do is download the latest JDK onto your machine.

Next you need set the path variable. I am using windows, so this is how you set.

control panel -> system – > advance setting -> environment variable – > system variables

select path and add new entry C:\Program Files\Java\jdk1.8.0_91\bin

Once you have done the above step. Open your command prompt and type javac
if that is working then the path variable is set correctly.

Use your favorite text editor to create a new file. You don’t need any advanced IDE like Eclipse or IntelliJ yet.
For the moment, stick to the editor you’re used to. Copy/paste the following code in your editor :

public class MooseGreetings {
 public static void main(String[] args) {

Save the file in the directory of your choice
The filename must match exactly the name of your class.

Compiling the program

At the command prompt, go to the directory where you have saved the file.

C:\>cd \java\src

The class should be compiled, and a new file called MooseGreetings.class should be created in the same directory.

Executing the program

C:\java\src>java -classpath . MooseGreetings

The –classpath flag (or -cp flag) may be used at the command prompt to tell Java where to look for .class files. In our example, “-classpath .” means that Java should scan the current directory (“.”) for class files. These can be set in the classpath either by setting the CLASSPATH environment variable, or by using the -classpath flag.

Note –  I will be adding newer posts with further versions like 2 , 3 etc till i have learned JAVA well. This is my way of making notes.


Head First Java 2nd Edition



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s