classpath
classpath 是 java 中用于指定可执行程序或类加载器 查找文件的路径
,它是一个一个环境变量,可以包含一个或多个目录或 jar 文件的路径。当 java 程序运行时,JVM 会根据 classpath 来查找所需的 class 文件
因为 java 是编译型语言,源码文件是 .java
,而编译后的 .class
文件才是真正可以被 JVM 执行的字节码。因此,JVM 需要知道,如果要加载一个 abc.xyz.Hello
的类,应该去哪搜索对应的 Hello.class
文件。所以 classpath 就是一组 class 文件目录的集合
配置
配置 classpath 的方式有两种
- 在系统环境变量中配置 classpath
- 在 JVM 启动时通过
-classpath
或 -cp
配置
第一种方式会污染整个系统环境,并不提倡该方式
假如需要运行一个 io.bibyte.Venom
的类,如果并不清楚这个类的 class 文件具体在哪的目录下,可以指定多个目录:java -cp .;D:\workspace;D:\project
如果是 Linux 环境,多个目录需要用 :
分割
java -cp .:/opt/workspace:/opt/project
当 JVM 在加载这个类时,会依次查找:
- .\io\bibyte\Venom.class
- D:\workspace\io\bibyte\Venom.class
- D:\project\io\bibyte\Venom.class
如果 JVM 在某个路径下找到了对应的 class 文件,就不会往后搜索。如果所有路径下都没有找到,就会抛出 java.lang.ClassNotFoundException
jar
JAR(Java Archive)是一种用于打包和分发 java 类、资源文件和元数据的文件格式。它类似于其他操作系统中的压缩文件格式(如ZIP),但专门用于Java应用程序
简单来说,jar 实际上就是一个压缩包,里面放着各个目录下的文件(包括 class 文件和其他文件)
所以 classpath 也可以指定一个或多个 jar 包,这样 JVM 就会到这些 jar 中去搜索需要加载的类
java -cp ./hello.jar io.bibyte.Venom
jar 包还可以包含一个特殊的 /META-INF/MANIFEST.MF
文件,MANIFEST.MF
是纯文本,可以指定 Main-Class
和其它信息。JVM 会自动读取这个 MANIFEST.MF
文件,如果存在 Main-Class
,我们就不必在命令行指定启动的类名,而是用更方便的命令:java -jar hello.jar
在大型项目中,不可能手动编写 MANIFEST.MF
文件,再手动创建 zip 包。Java 社区提供了大量的开源构建工具,例如 Maven,可以非常方便地创建 jar 包
P.S.
在 IDEA 中启动项目时,IDEA 会默认指定 classpath,里面包括了项目所依赖的所有 jar 包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| D:\workspace\jdk21\bin\java.exe "-javaagent:D:\software\IDEA\IntelliJ IDEA 2023.3.1\lib\idea_rt.jar=51005:D:\software\IDEA\IntelliJ IDEA 2023.3.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\project\dgm\dgm-core\target\test-classes; D:\project\dgm\dgm-core\target\classes; D:\workspace\repository\org\springframework\boot\spring-boot-starter-web\3.2.0\spring-boot-starter-web-3.2.0.jar; D:\workspace\repository\org\springframework\boot\spring-boot-starter\3.2.0\spring-boot-starter-3.2.0.jar; D:\workspace\repository\org\springframework\boot\spring-boot\3.2.0\spring-boot-3.2.0.jar; D:\workspace\repository\org\springframework\boot\spring-boot-autoconfigure\3.2.0\spring-boot-autoconfigure-3.2.0.jar; D:\workspace\repository\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar; D:\workspace\repository\org\yaml\snakeyaml\2.2\snakeyaml-2.2.jar; D:\workspace\repository\org\springframework\boot\spring-boot-starter-json\3.2.0\spring-boot-starter-json-3.2.0.jar; D:\workspace\repository\com\fasterxml\jackson\core\jackson-databind\2.15.3\jackson-databind-2.15.3.jar; D:\workspace\repository\com\fasterxml\jackson\core\jackson-annotations\2.15.3\jackson-annotations-2.15.3.jar; D:\workspace\repository\com\fasterxml\jackson\core\jackson-core\2.15.3\jackson-core-2.15.3.jar; D:\workspace\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.15.3\jackson-datatype-jdk8-2.15.3.jar; D:\workspace\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.15.3\jackson-datatype-jsr310-2.15.3.jar; D:\workspace\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.15.3\jackson-module-parameter-names-2.15.3.jar; D:\workspace\repository\org\springframework\boot\spring-boot-starter-tomcat\3.2.0\spring-boot-starter-tomcat-3.2.0.jar; D:\workspace\repository\org\apache\tomcat\embed\tomcat-embed-core\10.1.16\tomcat-embed-core-10.1.16.jar; D:\workspace\repository\org\apache\tomcat\embed\tomcat-embed-el\10.1.16\tomcat-embed-el-10.1.16.jar; D:\workspace\repository\org\apache\tomcat\embed\tomcat-embed-websocket\10.1.16\tomcat-embed-websocket-10.1.16.jar; D:\workspace\repository\org\springframework\spring-web\6.1.1\spring-web-6.1.1.jar; D:\workspace\repository\org\springframework\spring-beans\6.1.1\spring-beans-6.1.1.jar; D:\workspace\repository\io\micrometer\micrometer-observation\1.12.0\micrometer-observation-1.12.0.jar; D:\workspace\repository\io\micrometer\micrometer-commons\1.12.0\micrometer-commons-1.12.0.jar; D:\workspace\repository\org\springframework\spring-webmvc\6.1.1\spring-webmvc-6.1.1.jar; D:\workspace\repository\org\springframework\spring-aop\6.1.1\spring-aop-6.1.1.jar; D:\workspace\repository\org\springframework\spring-context\6.1.1\spring-context-6.1.1.jar; D:\workspace\repository\org\springframework\spring-expression\6.1.1\spring-expression-6.1.1.jar; D:\workspace\repository\ch\qos\logback\logback-classic\1.4.14\logback-classic-1.4.14.jar; D:\workspace\repository\ch\qos\logback\logback-core\1.4.14\logback-core-1.4.14.jar; D:\workspace\repository\org\slf4j\slf4j-api\2.0.9\slf4j-api-2.0.9.jar; D:\workspace\repository\org\springframework\boot\spring-boot-starter-test\3.2.0\spring-boot-starter-test-3.2.0.jar; D:\workspace\repository\org\springframework\boot\spring-boot-test\3.2.0\spring-boot-test-3.2.0.jar; D:\workspace\repository\org\springframework\boot\spring-boot-test-autoconfigure\3.2.0\spring-boot-test-autoconfigure-3.2.0.jar; D:\workspace\repository\com\jayway\jsonpath\json-path\2.8.0\json-path-2.8.0.jar; D:\workspace\repository\jakarta\xml\bind\jakarta.xml.bind-api\4.0.1\jakarta.xml.bind-api-4.0.1.jar; D:\workspace\repository\jakarta\activation\jakarta.activation-api\2.1.2\jakarta.activation-api-2.1.2.jar; D:\workspace\repository\net\minidev\json-smart\2.5.0\json-smart-2.5.0.jar; D:\workspace\repository\net\minidev\accessors-smart\2.5.0\accessors-smart-2.5.0.jar; D:\workspace\repository\org\ow2\asm\asm\9.3\asm-9.3.jar; D:\workspace\repository\org\assertj\assertj-core\3.24.2\assertj-core-3.24.2.jar; D:\workspace\repository\net\bytebuddy\byte-buddy\1.14.10\byte-buddy-1.14.10.jar; D:\workspace\repository\org\awaitility\awaitility\4.2.0\awaitility-4.2.0.jar; D:\workspace\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar; D:\workspace\repository\org\junit\jupiter\junit-jupiter\5.10.1\junit-jupiter-5.10.1.jar; D:\workspace\repository\org\junit\jupiter\junit-jupiter-api\5.10.1\junit-jupiter-api-5.10.1.jar; D:\workspace\repository\org\opentest4j\opentest4j\1.3.0\opentest4j-1.3.0.jar; D:\workspace\repository\org\junit\platform\junit-platform-commons\1.10.1\junit-platform-commons-1.10.1.jar; D:\workspace\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar; D:\workspace\repository\org\junit\jupiter\junit-jupiter-params\5.10.1\junit-jupiter-params-5.10.1.jar; D:\workspace\repository\org\junit\jupiter\junit-jupiter-engine\5.10.1\junit-jupiter-engine-5.10.1.jar; D:\workspace\repository\org\junit\platform\junit-platform-engine\1.10.1\junit-platform-engine-1.10.1.jar; D:\workspace\repository\org\mockito\mockito-core\5.7.0\mockito-core-5.7.0.jar; D:\workspace\repository\net\bytebuddy\byte-buddy-agent\1.14.10\byte-buddy-agent-1.14.10.jar; D:\workspace\repository\org\objenesis\objenesis\3.3\objenesis-3.3.jar; D:\workspace\repository\org\mockito\mockito-junit-jupiter\5.7.0\mockito-junit-jupiter-5.7.0.jar; D:\workspace\repository\org\skyscreamer\jsonassert\1.5.1\jsonassert-1.5.1.jar; D:\workspace\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar; D:\workspace\repository\org\springframework\spring-core\6.1.1\spring-core-6.1.1.jar; D:\workspace\repository\org\springframework\spring-jcl\6.1.1\spring-jcl-6.1.1.jar; D:\workspace\repository\org\springframework\spring-test\6.1.1\spring-test-6.1.1.jar; D:\workspace\repository\org\xmlunit\xmlunit-core\2.9.1\xmlunit-core-2.9.1.jar; D:\workspace\repository\org\projectlombok\lombok\1.18.30\lombok-1.18.30.jar; D:\workspace\repository\com\alibaba\fastjson2\fastjson2\2.0.43\fastjson2-2.0.43.jar; D:\workspace\repository\io\netty\netty-all\4.1.104.Final\netty-all-4.1.104.Final.jar; D:\workspace\repository\io\netty\netty-buffer\4.1.101.Final\netty-buffer-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec\4.1.101.Final\netty-codec-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-dns\4.1.101.Final\netty-codec-dns-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-haproxy\4.1.101.Final\netty-codec-haproxy-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-http\4.1.101.Final\netty-codec-http-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-http2\4.1.101.Final\netty-codec-http2-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-memcache\4.1.101.Final\netty-codec-memcache-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-mqtt\4.1.101.Final\netty-codec-mqtt-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-redis\4.1.101.Final\netty-codec-redis-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-smtp\4.1.101.Final\netty-codec-smtp-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-socks\4.1.101.Final\netty-codec-socks-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-stomp\4.1.101.Final\netty-codec-stomp-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-codec-xml\4.1.101.Final\netty-codec-xml-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-common\4.1.101.Final\netty-common-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-handler\4.1.101.Final\netty-handler-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport-native-unix-common\4.1.101.Final\netty-transport-native-unix-common-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-handler-proxy\4.1.101.Final\netty-handler-proxy-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-handler-ssl-ocsp\4.1.101.Final\netty-handler-ssl-ocsp-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-resolver\4.1.101.Final\netty-resolver-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-resolver-dns\4.1.101.Final\netty-resolver-dns-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport\4.1.101.Final\netty-transport-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport-rxtx\4.1.101.Final\netty-transport-rxtx-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport-sctp\4.1.101.Final\netty-transport-sctp-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport-udt\4.1.101.Final\netty-transport-udt-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport-classes-epoll\4.1.101.Final\netty-transport-classes-epoll-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport-classes-kqueue\4.1.101.Final\netty-transport-classes-kqueue-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-resolver-dns-classes-macos\4.1.101.Final\netty-resolver-dns-classes-macos-4.1.101.Final.jar; D:\workspace\repository\io\netty\netty-transport-native-epoll\4.1.101.Final\netty-transport-native-epoll-4.1.101.Final-linux-x86_64.jar; D:\workspace\repository\io\netty\netty-transport-native-epoll\4.1.101.Final\netty-transport-native-epoll-4.1.101.Final-linux-aarch_64.jar; D:\workspace\repository\io\netty\netty-transport-native-epoll\4.1.104.Final\netty-transport-native-epoll-4.1.104.Final-linux-riscv64.jar; D:\workspace\repository\io\netty\netty-transport-native-kqueue\4.1.101.Final\netty-transport-native-kqueue-4.1.101.Final-osx-x86_64.jar; D:\workspace\repository\io\netty\netty-transport-native-kqueue\4.1.101.Final\netty-transport-native-kqueue-4.1.101.Final-osx-aarch_64.jar; D:\workspace\repository\io\netty\netty-resolver-dns-native-macos\4.1.101.Final\netty-resolver-dns-native-macos-4.1.101.Final-osx-x86_64.jar; D:\workspace\repository\io\netty\netty-resolver-dns-native-macos\4.1.101.Final\netty-resolver-dns-native-macos-4.1.101.Final-osx-aarch_64.jar; D:\workspace\repository\org\sqids\sqids\0.1.0\sqids-0.1.0.jar net.venom.core.Test
|
Thanks
classpath和jar - 廖雪峰的官方网站 (liaoxuefeng.com)