亲宝软件园·资讯

展开

java bootclasspath的具体用法

sayyy 人气:0

前言

查看 bootclasspath

创建项目BootstrapClassPath,代码如下:

import java.io.File;

public class Test {
    public static void main(String[] args) {
        printPath("sun.boot.class.path");
    }
    
    public static void printPath(String name) {
        System.out.println(name + ":");
        String[] paths = System.getProperty(name).split(File.pathSeparator);
        for(String path : paths) {
            System.out.println("- " + path);
        }
    }
}

执行结果:

sun.boot.class.path:
- C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar
- C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar
- C:\Program Files\Java\jdk1.8.0_144\jre\lib\sunrsasign.jar
- C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar
- C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar
- C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar
- C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar
- C:\Program Files\Java\jdk1.8.0_144\jre\classes

修改bootclasspath

-Xbootclasspath/a 示例

cmd> java -Xbootclasspath/a:.\lib\xxx.jar;BootstrapClassPath.jar test.Test
sun.boot.class.path:
- C:\Program Files\Java\jre1.8.0_144\lib\resources.jar
- C:\Program Files\Java\jre1.8.0_144\lib\rt.jar
- C:\Program Files\Java\jre1.8.0_144\lib\sunrsasign.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jsse.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jce.jar
- C:\Program Files\Java\jre1.8.0_144\lib\charsets.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jfr.jar
- C:\Program Files\Java\jre1.8.0_144\classes
- ./lib/xxx.jar
- BootstrapClassPath.jar

-Xbootclasspath/p 示例

cmd> java -Xbootclasspath/p:.\lib\xxx.jar;BootstrapClassPath.jar test.Test
sun.boot.class.path:
- ./lib/xxx.jar
- BootstrapClassPath.jar
- C:\Program Files\Java\jre1.8.0_144\lib\resources.jar
- C:\Program Files\Java\jre1.8.0_144\lib\rt.jar
- C:\Program Files\Java\jre1.8.0_144\lib\sunrsasign.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jsse.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jce.jar
- C:\Program Files\Java\jre1.8.0_144\lib\charsets.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jfr.jar
- C:\Program Files\Java\jre1.8.0_144\classes

(可以)添加.class文件目录

cmd> java -Xbootclasspath/a:.\target\classes test.Test
sun.boot.class.path:
- C:\Program Files\Java\jre1.8.0_144\lib\resources.jar
- C:\Program Files\Java\jre1.8.0_144\lib\rt.jar
- C:\Program Files\Java\jre1.8.0_144\lib\sunrsasign.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jsse.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jce.jar
- C:\Program Files\Java\jre1.8.0_144\lib\charsets.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jfr.jar
- C:\Program Files\Java\jre1.8.0_144\classes
- .\target\classes

(可以)添加.yml、.xml、.properties(或其它配置文件)文件目录

cmd> java -Xbootclasspath/a:.\target\conf test.Test
sun.boot.class.path:
- C:\Program Files\Java\jre1.8.0_144\lib\resources.jar
- C:\Program Files\Java\jre1.8.0_144\lib\rt.jar
- C:\Program Files\Java\jre1.8.0_144\lib\sunrsasign.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jsse.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jce.jar
- C:\Program Files\Java\jre1.8.0_144\lib\charsets.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jfr.jar
- C:\Program Files\Java\jre1.8.0_144\classes
- .\target\conf

(可以)添加.jar文件

cmd> java -Xbootclasspath/a:.\target\BootstrapClassPath.jar test.Test
sun.boot.class.path:
- C:\Program Files\Java\jre1.8.0_144\lib\resources.jar
- C:\Program Files\Java\jre1.8.0_144\lib\rt.jar
- C:\Program Files\Java\jre1.8.0_144\lib\sunrsasign.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jsse.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jce.jar
- C:\Program Files\Java\jre1.8.0_144\lib\charsets.jar
- C:\Program Files\Java\jre1.8.0_144\lib\jfr.jar
- C:\Program Files\Java\jre1.8.0_144\classes
- .\target\BootstrapClassPath.jar

(不可以)添加.jar文件所在目录

cmd> java -Xbootclasspath/a:.\target test.Test

错误: 找不到或无法加载主类 test.Test

扩展知识

自JDK 1.2以后,JVM采用了委托(delegate)模式来载入class.
采用这种设计的原因可以参考: http://java.sun.com/docs/books/tutorial/ext/basics/load.html

加载全部内容

相关教程
猜你喜欢
用户评论