Saturday, May 10, 2014

Packages in java (2/2) - Setting the CLASSPATH

This is the 2nd article on packages.

We'll be discussing a simple "Hello world!" program with packages.

CLASSPATH settings

You do not need to set your CLASSPATH for this exercise. But we will have a look at it.

Windows: Go to My Computer > Right Click > Properties > Advanced tab > Environment variables > System variables (in the lower half)

If you don't find the entry CLASSPATH, don't bother

If set, your CLASSPATH settings must positively contain '.' (period). This is the default value of CLASSPATH and must be added when and if you decide to add more directories to it.

The entries in the CLASSPATH are separated by semi-colons.

Now you are all set to code.

First, download the source file here.

//Hello world with packages
//Simple program that says "Hello world!"
//with packages
//First line of code should be package statement:

class Hello{
  public static void main(String... args){
System.out.println("Hello world!");

Basically the package statement specifies the directory tree.

Place the com folder in the root directory (say D: drive)

Run javac and java commands:

You should get the above output. Note that if CLASSPATH is defined, there should a period in there as it tells the compiler to look in the current directory.

No comments :

Post a Comment