Classpath and Importing standard packages in Java

Class path is a relevance of Operating system rather than java, but it plays an important role for JVM. Standard packages are defualt java packages which helps programmers to go easy.


The class path is a way for JVM to know from where to invoke the execution of a class. Or where to find the specified class. Usually the class path is set to point the current directory. And Moreover the class path is more of a Operating System based concept it has a very less direct link to Java. However it is really important that you have defined a proper CLASSPATH in your environment variables of Operating System.

The Classpath is actually important because it tells the compiler or JVM where to find the required class. There is a reason that in a properly packaged software system the class with the main method is placed in the directory where it is completely accessible without any package restriction. To understand this reason clearly first look at following program.

place this file in packagedemo directory and name it
package packagedemo;
class PackageDemo{
	public static void main(String[] args){
		System.out.println("hello world");

Now you'll need to compile this and you'll get a class file under directory packagedemo. Now try and run it and see what Happens.

Exception in thread "main" java.lang.NoClassDefFoundError: PackageDemo (wrong name: packagedemo/PackageDemo)

Does your system shows this Exception too ? If so, you are going in right direction. Because you need the package reference to run this program as this is in the java package and classpath restriction applies on this one too.

you need to come out from the packagedemo and then run it from here using following command

java packagedemo.PackageDemo

this will result in output

hello world

Well, yeah, this is Classpath all about to tell the JVM where it stands and where to find what.
The reason the class with main method places outside the package restriction is only to make it accessible without this limitation.

Standard packages

The standard packages are commonly known as language package and classes which are shipped with the JDK. there are lots of common packages like


and many more..

these are the core part of Java Language specification. The purpose of these packages is to make it easier for programmer to do work and leave the additional hardware support responsibility to JVM. The java.lang is already included in every program you make so you don't need any special operation to work on this but if you want to use any of other package or class from it, you will be needing to import the package or any class from that package. for example if you want to work with the Random class from java.util, you must use any of these approach:

import java.util.*;
class UseRandom{
	private Random r;
	//class Definition


import java.util.Random;
class UseRandom{
	private Random r;
	//class definition

I hope this little illustration is enough to make you understand how to import and use standard packages.

Note: using private with the variable fields is not a rule but is a good practice. It is a good practice in OOP and specially java to keep data private and access it through the public methods commonly known as getter and setters.