Java – Set Classpath from Command Line

Learn to use the -classpath or -cp option to set the Java classpath from the command prompt in Windows and Linux OS.

学习在Windows和Linux操作系统中使用-classpath或-cp选项在命令提示符中设置Java类路径。

1. Java Classpath

  • The classpath is the list of directory locations that the Java runtime environment searches for the classes and other resource files, during program execution.类路径是Java运行时环境在程序执行期间搜索类和其他资源文件的目录位置列表。
  • The default classpath is the current working directory.默认的类路径是当前工作目录。
  • Setting the CLASSPATH environment variable or using the -classpath command-line option overrides the default value.设置CLASSPATH环境变量或使用-classpath命令行选项将覆盖默认值。
  • If we want to include the current working directory in the classpath, we must include "." (dot or period character) in the list of paths.如果我们想在类路径中包含当前工作目录,我们必须包含"."(点或句号字符)在路径列表中。
  • The value of classpath set from the command prompt is temporary and is not available if we open a new command prompt.从命令提示符设置的classpath值是临时的,如果我们打开一个新的命令提示符,则不可用。
  • If we want to set the classpath permanently then we should set the classpath as environment variable in Windows. In Linux, set the value in bash profile.


    如果我们想要永久设置类路径,那么我们应该将类路径设置为Windows中的环境变量。Linux操作系统下,在bash profile中设置。

Path Separators are Platform Dependent路径分隔符依赖于平台

The separator between two paths in the classpath value is platform-dependent.

classpath值中两个路径之间的分隔符与平台相关。 

In Windows, the path separator is semicolon ( ; ) and the path separator in Linux is colon ( : ).

在Windows中,路径分隔符是分号(;)Linux下,路径分隔符为冒号(:)。

2. Setting Classpath from Command Line从命令行设置类路径

There are two ways :

  • Use -classpath or -cp option to provide the classpath locations while starting the Java application or tool.在启动Java应用程序或工具时,使用-classpath或-cp选项提供类路径位置。
java -cp .;c:/jars demo-application.jar
  • Use set CLASSPATH command initially, and then run Java application or tool in the same command line window. “首先使用set CLASSPATH命令,然后在同一命令行窗口中运行Java应用程序或工具。
    It will search the classes/resources in mentioned classpath locations.
    Classpath entries that are neither directories nor archives (.zip or .jar files) nor * are ignored.它将在提到的类路径位置搜索类/资源。
    既不是目录也不是归档文件(.zip或.jar文件)也不是*的类路径条目将被忽略。
set CLASSPATH .;c:/jars

The above examples are specific to Windows. Please use colon as the path separator for Linux.上面的例子是特定于Windows的。请使用冒号作为Linux的路径分隔符。

3. Wildcards in Classpath Classpath中的通配符

Generally, we will include two kinds of resources in classpath i.e. classes and jar files.通常,我们将在类路径中包含两种资源,即类和jar文件。

  • To include classes, give the complete path of the folder where classes are present. . (dot) represent the current directory.若要包含类,请给出存在类的文件夹的完整路径。.(点)表示当前目录。
  • To include jar files, give the complete path of the folder where jars are present. Add a star (*) wildcard for adding all jar files in that directory.要包含jar文件,请给出存放jar文件的文件夹的完整路径。添加一个星号(*)通配符来添加该目录中的所有jar文件。

Please note that the jar files present in the sub-directories are NOT included automatically. Sub-folders must be included explicitely.请注意,子目录中的jar文件不会自动包含在内。子文件夹必须显式包含。

java -cp c:/temp/lib applation.jar				        //includes all classes in the directory 'c:/temp/lib'

java -cp c:/temp/lib/* application.jar				//includes all jars in the directory 'c:/temp/lib'

java -cp c:/temp/lib;c:/temp/lib/*	application.jar       //includes all classes and jars

4. How to Clear Classpath Value如何清除类路径值

To unset a classpath value previously set, use this command to clear its value using am empty value assigned to the variable.要取消先前设置的类路径值,使用此命令通过分配变量空值清除类路径环境变量。

set CLASSPATH=

Further Reading:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无心六神通

你的鼓励是我持续创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值