How to add the JAR file to Classpath in Java
Adding JAR into classpath is a common task for Java programmers and different programmers do it in different ways. Since Java allows multiple ways to include JAR files in the classpath, it becomes important to know the pros and cons of each approach and How exactly they work. There are 5 ways to add jars into classpath in Java some of them we have already seen in How classpath works in Java and How to set Path in Java. In this post, we will revisit some of those techniques and explore Java 1.6 wildcard to add multiple JAR into the classpath.
In short knowledge of Path, Classpath and Classloaders are a must for any Java programmer, not only from a knowledge point of view but also to successfully debug and resolve issues related to Classpath e.g. NoClassDefFoundError and java.lang.ClassNotFoundException in Java.
5 ways to add multiple JARs in Classpath
Here are some of the ways you can add jar files in the classpath of a Java program :1. Include the JAR name in the CLASSPATH environment variable.
The CLASSPATH environment variable is case insensitive and it can be either Classpath or classpath as well. This is similar to a PATH environment variable which is used to locate Java binaries e.g. javaw and java command. By the way here are 5 ways to add the JAR file into the classpath.
2. Include the name of the 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 can contain a list of Jar files required by your application.
Another advantage of using –classpath command-line option is that, it allows every application to have its own set of JAR in the classpath, unlike the previous option which is available to all Java programs running on the same host.
3. Include the jar name in the Class-Path option in the manifest.
If you are running an executable JAR file, you might have noticed the Class-Path attribute in the manifest file inside the META-INF folder. The Class-Path option takes the highest priorities and overrides both the CLASSPATH environment variable and the –classpath command-line option. This is also a good place to include all JAR files required by Java applications.
4. Use Java 6 wildcard option to include multiple JAR
From Java 1.6+ onwards you can use a wildcard to include all jars in a directory into the set classpath or provide it to the Java program directly using -classpath command-line option. Following Java, the command example shows how to add multiple JAR into classpath using Java 6 wildcard method.
$ java.exe -classpath E:\lib\* Main
This is the newest option of adding multiple JAR files into the classpath. The above command will include all JAR files inside the E:\lib directory into the classpath. One thing which is worth noting while using a wildcard to include multiple JAR is that syntax must be correct.
In fact, that’s a common mistake many Java programmers make. Here are few more important points about using Java 6 wildcard to include multiple JAR in classpath :
1) In order to include all JAR from a directory you need to use the wildcard * and not *.jar
2) If you have JAR and class files in the same directory then you need to include each of them separately. Wildcard only matches JAR files and not classes. E.g.
$ java –classpath /classes;/lib/*
3) Java 6 wildcard to include all JAR will not search for JARs in a subdirectory.
4) One more important point is that wildcard to include all JAR is not honored in case if you are running a Java program with a JAR file and that have Class-Path attribute in the manifest file. JAR wildcard is honored If you use the –cp or –classpath option. See Core Java for Impatient for more details:
5. Adding JAR in ext directory e.g. C:\Program Files\Java\jdk1.6.0\jre\lib\ext
This is another way you can add 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. By the way, I don’t suggest this way of adding JAR into Classpath as it's not standard, it is just for information purposes.
These were a couple of ways you can include JAR files in your Classpath. Java 6 wildcard is the preferred way of adding multiple JAR in classpath along with the –cp and –classpath command-line option. If you are working in multiple operating systems then it's also good to remember that two directories in classpath are separated using a semicolon(;) in windows and with colon : in UNIX based system.
Other Java fundamental Tutorials
5 comments :
The opposite of this problem is quite a bigger one: Given classpath, for instance inside an application server like jboss, or inside an IDE like eclipse, how can I determine,from which part of this given classpath a certain class comes from?
To add JAR in eclipse classpath, if we are using Maven, the command "mvn eclipse:eclipse" will populate classpath and if we are using Gradle, command "gradle eclipse" will populate classpath
Does not work! There is no folder called "C:\", and the correct seperator character is a forward slash (/)! Where is the default classpath?
If you are using the Class-Path option in the manifest file, what is the syntax for multiple jar files contained, for example, in a lib folder within the jar file?
Sorry, I don't remember on top of my head but just separating them with comma should work, did you try that?
Post a Comment