java bootclasspath的具体用法
sayyy 人气:0前言
- jdk1.8
查看 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: 完全取代基本核心的Java class 搜索路径.不常用(慎用),否则要重新写所有Java 核心class
- -Xbootclasspath/a: 后缀。在bootclasspath后面添加。常用!!
- -Xbootclasspath/p: 前缀。在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
- ./lib/xxx.jar;BootstrapClassPath.jar 被添加到了sun.boot.class.path后面
- 前面的源码打包到了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
- ./lib/xxx.jar;BootstrapClassPath.jar 被添加到了sun.boot.class.path前面
- 前面的源码打包到了BootstrapClassPath.jar中
(可以)添加.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
加载全部内容