Rabu, 28 Mei 2014

5 ways to add multiple JAR in to Classpath in Java




How to add JAR file to Classpath in Java


Adding JAR into classpath is a common task for Java programmer and
different programmer do it on different way. Since Java allows multiple ways to
include JAR file in classpath, it becomes important to know pros and cons of
each approach and How exactly they work. There are 5 ways to add
 jars on 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 classpath. By the way here
is 5 ways to add
JAR
file into classpath. In short knowledge of Path, Classpath and Classloaders
are must for any Java programmer, not only from 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 classpath of a Java program :







1.
Include the JAR name in CLASSPATH environment variable.




CLASSPATH environment variable is case insensitive and it can be either Classpath or classpath as well.
This is similar to PATH environment variable which is used to locate Java
binaries e.g. javaw
and java command.











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




How to add JAR File into Java Classpath with ExampleThis 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 list of Jar file 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 classpath unlike previous option which is available to all
Java program running on 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 Class-Path attribute
in manifest file inside META-INF folder. Class-Path  option takes highest priorities and overrides
both CLASSPATH environment variable and –classpath command
line option. This is also a good place to include all JAR file required by
Java application.











4. 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 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 file into classpath.
Above command will include all JAR file inside E:\lib directory
into classpath. One thing which is worth noting while using wildcard to include multiple JAR is that syntax mut
be correct. In fact that’s a common
mistake many Java programmer make
. Here is few more important points about
using Java 6 widlcard to include multiple
JAR in classpath
:





1) In order to include all JAR from a directory you need to use wildcard
* and not  *.jar







2) 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/*







3) Java 6 wildcard to inlcude all JAR will not search for JARs in
subdirectory.







4) 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.
  JAR wildcard is honored If you use –cp or –classpath option.











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 its not standard, it just for information purpose.







These were couple of ways you can include 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. If you are working in 
multiple operating
system than its also good to remember that two directories in classpath are 
separated using semi colon(;) in windows and with colon : in UNIX based system.





Other Java fundamental Tutorials





























Source:http://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html

Tidak ada komentar:

Posting Komentar