classpath is a parameter—set either on the command-line, or through an environment variable—that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.

Overview and architecture

Similar to the classic dynamic loading behavior, when executing Java programs, the Java Virtual Machine finds and loads classes lazily (it loads the bytecode of a class only when this class is first used). The classpath tells Javawhere to look in the filesystem for files defining these classes.

The virtual machine searches for and loads classes in this order:

bootstrap classes: the classes that are fundamental to the Java Platform (comprising the public classes of the Java Class Library, and the private classes that are necessary for this library to be functional).

extension classes: packages that are in the extension directory of the JRE or JDK, jre/lib/ext/

user-defined packages and libraries

By default only the packages of the JDK standard API and extension packages are accessible without needing to set where to find them. The path for all user-defined packages and libraries must be set in the command-line (or in the Manifest associated with the Jar file containing the classes).

Setting the path to execute Java programs

Supplying as application argument

Suppose we have a package called org.mypackage containing the classes:

HelloWorld (main class)

SupportClass

UtilClass

and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).

The file structure will look like this:

D:\myprogram\

|

---> org\

|

---> mypackage\

|

---> HelloWorld.class

---> SupportClass.class

---> UtilClass.class

/home/user/myprogram/

|

---> org/

|

---> mypackage/

|

---> HelloWorld.class

---> SupportClass.class

---> UtilClass.class

When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we use the following command:

java -classpath D:\myprogram org.mypackage.HelloWorld

java -classpath /home/user/myprogram org.mypackage.HelloWorld

where:

java is a java application launcher, a type of sdkTool(A command-line tool, such as java, javac, javadoc, or apt)

-classpath D:\myprogram sets the path to the packages used in the program (on Linux, -classpath /home/user/myprogram) and

org.mypackage.HelloWorld is the name of the main class

Setting the path through an environment variable

The environment variable named CLASSPATH may be alternatively used to set the classpath. For the above example, we could also use on Windows:

set CLASSPATH=D:\myprogram

java org.mypackage.HelloWorld

The rule is that -classpath option, when used to start the java application, overrides the CLASSPATH environment variable. If none are specified, the current working directory is used as classpath. This means that when our working directory is D:\myprogram\ (on Linux,/home/user/myprogram/), we would not need to specify the classpath explicitly. When overriding however, it is advised to include current folder "." into the classpath in the case when loading classes from current folder is desired.

The same applies not only to java launcher but also to javac, the java compiler.

Setting the path of a Jar file

If a program uses a supporting library enclosed in a Jar file called supportLib.jar, physically in the directory D:\myprogram\lib\ and the corresponding physical file structure is:

D:\myprogram\

|

---> lib\

|

---> supportLib.jar

|

---> org\

|

--> mypackage\

|

---> HelloWorld.class

---> SupportClass.class

---> UtilClass.class

the following command-line option is needed:

java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld

or alternatively:

set CLASSPATH=D:\myprogram;D:\myprogram\lib\supportLib.jar

java org.mypackage.HelloWorld

Adding all JAR files in a directory

In Java 6 and higher, one can add all jar-files in a specific directory to the classpath using wildcard notation.

Windows example:

java -classpath ".;c:\mylib\*" MyApp

Linux example:

java -classpath '.:/mylib/*' MyApp

This works for both -classpath options and environment classpaths.

Setting the path in a Manifest file

Suppose that a program has been enclosed in a Jar file called helloWorld.jar, put directly in the D:\myprogram directory. We have the following file structure:

D:\myprogram\

|

---> helloWorld.jar

|

---> lib\

|

---> supportLib.jar

The manifest file defined in this Jar file has this definition:

Main-Class: org.mypackage.HelloWorld

Class-Path: lib/supportLib.jar

It's important that the manifest file ends with either a new line or carriage return.

To launch the program, we can use the following command:

java -jar D:\myprogram\helloWorld.jar [app arguments]

This will automatically start the org.mypackage.HelloWorld specified in the Main-Class with the arguments and user cannot replace this class name using java -jar options. The Class-Path meantime describes the location of the supportLib.jar file relative to the location of thehelloWorld.jar. Neither absolute file path (which is permitted in -classpath parameter on the command line) nor jar-internal paths are supported. This particularly means that if main class file is contained in a jar, org/mypackage/HelloWorld.class must be a valid path on the root within the jar.

Multiple classpath entries are separated with spaces:

Class-Path: lib/supportLib.jar lib/supportLib2.jar

OS specific notes

Being closely associated with the file system, the command-line Classpath syntax depends on the operating system. For example:

on all Unix-like operating systems (such as Linux and Mac OS X), the directory structure has a Unix syntax, with separate file paths separated by a colon (":").

on Windows, the directory structure has a Windows syntax, and each file path must be separated by a semicolon (";").

This does not apply when the Classpath is defined in manifest files, where each file path must be separated by a space (" "), regardless of the operating system.

Diagnose

Application programmers may want to find out/debug the current settings under which the application is running:

System.getProperty("java.class.path")

linux 分隔符是冒号:    windows分隔符是分号;

将所以jar包的分号换成冒号即可

Java classpath 和 classpath引入和不引入星号(*) 区别:

classpath 不引入星号:如:D:\abc\ 只会到你的class路径中查找找文件;

classpath 引入星号*: 如:D:\abc\* 不仅包含class路径,还包括jar文件中(class路径)进行查找.

java cp classpath_java -classpath or -cp 的设置和解释相关推荐

  1. linux java -cp lt; .txt_补交 20155202 蓝墨云班课 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能...

    蓝墨云班课 编写MyCP.java 要求: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX ...

  2. Java中的classpath

    CLASSPATH是Java中最重要的概念之一,但通常都被忽视了.不清楚classpath就不会知道java如何定位你的类文件. CLASSPATH是一个环境变量,使Java定位用户定义的类.在Win ...

  3. 为什么jdk的CLASSPATH环境变量需要设置rt.jar 和 tools.jar

    How Classes are Found 中有说明:(java启动类文件在 rt.jar中, 而 工具类文件在 tools.jar 中)  How the Java Launcher Finds C ...

  4. linux cp{,bak},Linux中cp覆盖不提示

    cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,这在大量cp覆盖操作的时候是不能忍受的. 1. 把a目录下的文件复制到b目录 cp –r a/* b 2. 执行上面的命令时,b存在的每个文件都会 ...

  5. java .classpath配置_轻松玩转Java配置的Classpath

    和Java类路径(classpath)打交道的过程中,开发者偶尔会遇到麻烦.这是因为,类装载器实际装入的是哪一个类有时并不显而易见,当应用程序的classpath包含大量的类和目录时,情况尤其严重.本 ...

  6. 关于JAVA项目中CLASSPATH路径详解

    在dos下编译java程序,就要用到classpath这个概念,尤其是在没有设置环境变量的时候.classpath就是存放.class等编译后文件的路径. javac:如果当前你要编译的java文件中 ...

  7. 关于JAVA_HOME, CLASSPATH和PATH的设置

    http://bbs.csdn.net/topics/120079565 1.PATH,这个是给WINDOWS操作系统用的,告诉命令行里,执行的命令行工具在那里,比如java,javac这都是命令行工 ...

  8. Linux CentOS  复制文件、替换文件 cp 复制文件、cp 覆盖文件 cp替换文件

    Linux CentOS  复制文件.替换文件 cp 复制文件.cp 覆盖文件 cp替换文件 一.Linux 复制文件语法 1. cp [option] src dest 2. option 可选参数 ...

  9. java classpath 目录_关于JAVA项目中CLASSPATH路径详解

    在dos下编译java程序,就要用到classpath这个概念,尤其是在没有设置环境变量的时候.classpath就是存放.class等编译后文件的路径. javac:如果当前你要编译的java文件中 ...

最新文章

  1. Unity旋转问题的总结
  2. “外星人”字符串生成算法研究
  3. 计算机软考网络工程师历年真题,计算机软考《网络工程师》考试历年真题精选(1)...
  4. 字节跳动---毕业旅行问题
  5. 在php中array函数的作用是什么意思,php中的array函数有什么用
  6. [转载]程序员的激情其实是一种痛苦
  7. HDOJ 1114 Piggy-Bank 【动态规划 完全背包】
  8. netzapper操作
  9. 清除1188.com
  10. excel查看编码格式_Excel表格中格式转换的这些套路,你都get了吗?
  11. jquery发送ajax请求并设置请求头
  12. java ttf_java直接使用ttf字体,解决window和linux之间的差异
  13. 你可能不知道的21个PS技巧
  14. HTTP协议分析实验
  15. Lighthouse 激光定位技术开源了,但不是 Valve 做的
  16. 体外克隆技术的研究报告
  17. 【数据分析】使用numpy实现多项式的求导以及可视化
  18. 【Linux】软件安装(三分钟教会你如何在linux下安装软件)
  19. 远创机器人_雅马哈锁螺丝机器人,打标机器人
  20. Typroa标题自动编号(从一级目录或二级目录开始)

热门文章

  1. 分析到决策?“AI+BI”这辆车你得尽快搭上
  2. 服务器文件传输工具:FileZilla
  3. 电商开发:SPU、SKU、SKC、ARPU 分别代表的含义
  4. Arduino控制RGB三色LED灯实验、程序代码、连线图、仿真
  5. 【杂谈】珍惜最后的学校生活
  6. 基于c#的工厂订单管理系统的设计与实现
  7. 服务器日志管理系统,服务器日志管理系统
  8. Servlet | 访问不同格式文件(PDF、doc)
  9. Linux(N1小钢炮,树莓派,openwrt等)获取ipv6公网地址发送给自己微信
  10. 程序员的生存技巧 — 搜索技巧