java: lib & classpath

classpath, is the path to search for third-part or user-defined classes,

refer to:
* linux
    docs.oracle.com/javase/6/docs/technotes/tools/solaris/classpath.html
    docs.oracle.com/javase/6/docs/technotes/guides/extensions/extensions.html
    docs.oracle.com/javase/7/docs/technotes/tools/solaris/jdkfiles.html
* windows
    docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html
*

------
3 level of jars

include:
* java platform classes (bootstrap)
    location:
        jre_home/lib/
            rt.jar
            ..
    this will be searched automatically, load by bootstrap classloader,
* java extension
    location:
        jre_home/lib/ext/
    this will be searched automatically, load by extension classloader,
* third-party & user-defined
    this need to specify in classpath, load by system classloader,
*

search order:
    java platform classes -> java extension -> third-party & user-defined,

------
set classpath

2 ways to set:
* set CLASSPATH environment
* use -cp option
*

default value:
    classpath default to current dir,

override:
    both of them will override the default classpath,
    so add "." to include current dir, and add "$CLASSPATH" as prefix to include the original classpath,

search order:
    the search order of classpath, is the same as you specified in classpath,

------
classpath format

format:
    linux:
        path_1:path_2:..:path_N
    windows:
        path_1;path_2;..;path_N

path:
    each path could be jar/zip/dir, '.' means current path,
    the dir or root dir included by jar/zip, should include:
    * the top package folder, if class has package
    * .class file, if class has no package

wildcard:
    '*', used it at end of a path, after separator '/', it equals to include all files with extension '.jar' or '.JAR' in that folder,
    '*' can't be used in other format,
    e.g.
        # this will include all jars under "/tmp/lib"
        "/tmp/lib/*"

------
tools.jar & dt.jar

under jdk_home/lib, there are some jars, they are not part of java platform, but are useful tools,
if want to use them, need to include in classpath,

jars:
* tools.bar
    non-core classes for support of the tools and utilities in the JDK
* dt.jar
     the DesignTime archive of BeanInfo files that tell IDE how to display the Java components and how to let the developer customize them for an application,
*

------
e.g.

* config classpath - linux - eric.sh
    CLASSPATH=.:$JAVA_HOME/lib/*
* execute command by add additional path to configured classpath - linux
    java -cp $CLASSPATH:/mnt/star/workspace/java_workplace hello.Test
*
* set classpath via command temporary - linux shell
    # include a single jar, base on original classpath,
    export CLASSPATH=$CLASSPATH:/tmp/test.jar
    # include all jars under specified folder, base on original classpath,
    export CLASSPATH=$CLASSPATH:/tmp/lucene_demo/lib/*
*

------

你可能感兴趣的:(classpath)