This Java tutorial is to help Java beginners add a Java class or jar files to the Java classpath. java.lang.ClassNotFoundException is one of the frequently faced exceptions for a Java beginner.
ClassNotFoundException is thrown when the referred Java class is not found by the Java runtime. Following are the different ways to add a Java class file or jar file to the Java classpath,
Use echo %CLASSPATH% to know the current classpath that is set.
Set classpath to a jar in current folder:
java -classpath "mysql-connector-java-5.1.19-bin.jar" MyJavaClassName
Set classpath to multiple jar files:
java -cp
Set classpath to multiple jar files in same folder:
java -cp .;
Set classpath to class file in current folder:
java -cp .; MyJavaClassName
Separator ;
is for windows. :
is for Unix systems.
Copy required jar files to ext folder in the Java jdk installation:C:\Program Files\Java\jdk1.8.0\jre\lib\ext
Unlike the above, Jars placed in SystemRoot is available for all JDKs:
%SystemRoot%\Sun\Java\lib\ext
CLASSPATH environment variable can be set in multiple ways and it depends on the OS used. In Windows, it can be set using the command-line tool. When set using command-line tool, it will live until that command-line session is closed. Alternately, it can be set from System –> Properties.
set CLASSPATH =
Use separator (; for windows or : for Unix systems) to add multiple entries.
When a Jar file is dependent on other Jar files, then the dependent jar files list can be added to the required Jar file’s manifest entry.
jar cfm helloWorld.jar Manifest.txt com/javapapers/*.class
Comments are closed for "Java Classpath".
[…] We can create a Jar file of our custom classes and add it to the Java classpath. Refer the tutorial to add Java class or Jar file to classpath if you are a […]
You can also add how to do same in eclipse IDE also
zdc