java tutorial - How to add JAR file to Classpath in Java - java programming - learn java - java basics - java for beginners



Add JAR file to Classpath in Java

  • Here are some of the ways you can include jar files in classpath of a Java program:

Include the JAR name in CLASSPATH environment variable.

  • CLASSPATH environment variable is case insensitive and it can be either Classpath or classpath too. This is like PATH environment variable that is used to locate Java binaries e.g. javaw and java command.

Include name of JAR file in -classpath command line option.

  • This is the preferred option if you are passing -classpath option while running your Java program as java -classpath ${CLASSPATH} Main.
  • Here CLASSPATH shell variable contain list of Jar file required by your application. Another benefit of using -classpath command line option is that, it enables each and every application to have its own set of JAR in classpath not at all like previous option that is available to all Java program running on same host.

Include the jar name in the Class-Path option in the manifest

  • If you are running an executable JAR file, you might have noticed Class-Path attribute in manifest file inside META-INF folder. Class-Path alternative takes highest priorities and overrides CLASSPATH environment variable as well as -classpath command line option.
  • This is also a better place to add all JAR file required by Java application.

Use Java 6 wildcard option to include multiple JAR

  • Form Java 1.6+ onwards you can use wildcard to include all jars in a directory into the set classpath or provide it to Java program directly using -classpath command line option.
  • Following Java command illustration shows how to add multiple JAR into classpath using Java 6 wildcard method.
java.exe -classpath E:\lib\* Main
  • This is the most current option of including multiple JAR file into classpath. Above command will include every JAR file inside E:\lib directory into classpath.
  • One thing which is worth nothing while using wildcard to include multiple JAR is that syntax must be correct.
  • Here is few more important points about using Java 6 wildcard to include multiple JAR in classpath:
  • In order to include every JAR from a directory you require to use wildcard * and not *.jar
  • If you have JAR and class file in same directory than you need to include each of them separately.
  • Wildcard only match JAR files and not classes.
E.g. Java -classpath /classes;/lib/*
  • Java 6 wildcard to include all JAR will not search for JARs in subdirectory.
  • One more important point is that wildcard to include all JAR is not honored in case if you are running Java program with JAR file and that have Class-Path attribute in manifest file.

Adding JAR in ext directory e.g. C:\Program Files\Java\jdk1.6.0\jre\lib\ext

  • This is one more way you can include multiple JAR in your classpath.
  • JAR from ext directory is loaded by extension Classloader and it has higher priority than application class loader which loads JAR from either CLASSPATH environment variable or directories specified in -classpath or -cp option.
  • These were couple of ways you can add JAR files in your Classpath. Java 6 wildcard is preferred way of adding multiple JAR in class path along with -cp and -classpath command line option.
  • In the event that you are working in multiple OS than it is too fine to recall that two directories in classpath are divided using semi colon (;) in windows based systems and in UNIX based system colon (:) is used.

Related Searches to How to add JAR file to Classpath in Java