What is CLASSPATH in Java
In this tutorial we are going to learn about CLASSPATH in detail.
Classpath in Java is path to directory or list of directory which is used by ClassLoaders to find and load class in Java program. Classpath can be specified using CLASSPATH environment variable which is case insensitive, -cp or -classpath command line option or Class-Path attribute in manifest.mf file inside JAR file in Java. From below you can findout some importent points about java CLASSPATH
- Classpath in Java is an environment variable used by Java Virtual machine to locate or find class files in Java during class loading.
- You can override value of Classpath in Java defined by environment variable CLASSPATH by providing JVM command line option –cp or –classpath while running your application.
- If two classes with same name exist in Java Classpath then the class which comes earlier in Classpath will be picked by Java Virtual Machine.
- By default CLASSPATH in Java points to current directory denoted by ”.” and it will look for any class only in current directory.
- When you use the -jar command line option to run your program as an executable JAR, then the Java CLASSPATH environment variable will be ignored, and also the -cp and -classpath switches will be ignored and In this case you can set your java classpath in the META-INF/MANIFEST.MF file by using the Class-Path attribute.
- In Unix of Linux Java Classpath contains names of directory with colon ”:” separated , On Windows Java Classpath will be semi colon ”;” separated while if you defined java classpath in Manifest file those will be space separated.
- You can check value of classpath in java inside your application by looking at following system property “java.class.path” System.getProperty(“java.class.path”) Class-Path attribute is used to contain classpath inside manifest file. Also make sure that your manifest file must end with a blank line (carriage return or new line) , here is an example of java classpath in manifest file. Main-Class: com.classpathexample.Demo_Classpath Class-Path: lib/tibco.jar lib/log4j.jar
- It’s also important to note that path specified in manifest file is not absolute instead they are relative from application jar’s path. For example in above if your application jar file is in C:\test directory you must need a lib directory inside test and tibco.jar and log4j.jar inside that.
- ClassNotFoundException is an Exception and will be thrown when Java program dynamically tries to load a particular Class at Runtime and don’t find that on Java classpath due to result of Class.forName() or loadClass() method invocation.
- NoClassDefFoundError comes when a particular class was present in Java Classpath during compile time but not available during runtime on Classpath in Java.