Java Tutorial Setting the CLASSPATH
Now, suppose we place the package schoolClasses under the C:\ directory. We need to set the classpath to point to that directory so that when we try to run it, the JVM will be able to see where our classes are stored.
Before we discuss how to set the classpath, let us take a look at an example on what will happen if we don't set the classpath.
Suppose we compile and then run the StudentRecord class we wrote in the last section,
C:\schoolClasses>javac StudentRecord.java
C:\schoolClasses>java StudentRecord
Exception in thread "main" java.lang.NoClassDefFoundError: StudentRecord (wrong name: schoolClasses/StudentRecord)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
We encounter a NoClassDefFoundError which means that Java did not know where to look for your class. The reason for this is that your class StudentRecord now belongs to a package named studentClasses. If we want to run our class, we jave to tell Java about its full class name which is schoolClasses.StudentRecord. We also have to tell JVM where to look for our packages, which in this case is in location C:\. To do this, we must set the classpath.
To set the classpath in Windows, we type this at the command prompt,
C:\schoolClasses> set classpath=C:\
where C:\ is the directory in which we have placed the packages. After setting the classpath, we can now run our program anywhere by typing,
C:\schoolClasses> java schoolClasses.StudentRecord
For Unix base systems, suppose we have our classes in the directory
/usr/local/myClasses, we write,
export classpath=/usr/local/myClasses
Take note that you can set the classpath anywhere. You can also set more than one classpath, we just have to separate them by ;(for windows) and : (for Unix based systems). For example,Now, suppose we place the package schoolClasses under the C:\ directory. We need to set the classpath to point to that directory so that when we try to run it, the JVM will be able to see where our classes are stored.
Before we discuss how to set the classpath, let us take a look at an example on what will happen if we don't set the classpath.
Suppose we compile and then run the StudentRecord class we wrote in the last section,
C:\schoolClasses>javac StudentRecord.java
C:\schoolClasses>java StudentRecord
Exception in thread "main" java.lang.NoClassDefFoundError: StudentRecord (wrong name: schoolClasses/StudentRecord)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
We encounter a NoClassDefFoundError which means that Java did not know where to look for your class. The reason for this is that your class StudentRecord now belongs to a package named studentClasses. If we want to run our class, we jave to tell Java about its full class name which is schoolClasses.StudentRecord. We also have to tell JVM where to look for our packages, which in this case is in location C:\. To do this, we must set the classpath.
To set the classpath in Windows, we type this at the command prompt,
C:\schoolClasses> set classpath=C:\
where C:\ is the directory in which we have placed the packages. After setting the classpath, we can now run our program anywhere by typing,
C:\schoolClasses> java schoolClasses.StudentRecord
For Unix base systems, suppose we have our classes in the directory
/usr/local/myClasses, we write,
export classpath=/usr/local/myClasses
set classpath=C:\myClasses;D:\;E:\MyPrograms\Java
and for Unix based systems,
export classpath=/usr/local/java:/usr/myClasses
Related Post:
Java Tutorial switch statement source codeJava tutorial source code get input from keyboard
Java Tutorial Using JOptionPane to get input
Java tutorial Decision Control Structures: if statement
PHP Tutorial
Java programing source code sample printing characters
Java tutorial if-else statement
C programming sample source code: Sum of 2 numbers
Java Programming Features



