Java开发中常用到环境变量的配置,下面简单介绍下Java中经常配置的环境变量:PATH和CLASSPATH。

1、PATH环境变量

1.1 作用简介

安装完JDK(Java Development Kit,Java开发套件)之后,可以在安装目录下找到两个子目录(bin目录和lib目录)。bin目录中包含着Java编译器等可执行文件。

如果要运行执行java命令,必须得执行java命令对应的可执行文件的路径,通常有两种方式:

  • 在%JAVA_HOME%目录下执行。
  • 执行命令的时候,指明路径%JAVA_HOME%/bin/java

但是,这样不是特别方便,这就是为什么配置环境变量。如果将%JAVA_HOME%/bin/,添加到环境变量PATH中。再执行java命令时(无论在哪个目录下执行),系统就会从左到右搜索(这里的顺序很重要,可以利用这个特性覆盖掉某个旧版本的jdk。)环境变量PATH中执行的目录,直到找到对应的可执行文件并执行(找到之后,后面的目录都会被忽略掉)。如果找不到,提示该命令不存在。这就是PATH环境变量的作用。

1.2 如何配置

另外,由于JDK的安装目录中的%JAVA_HOME%/jre/bin目录下也有一些常用的工具,所以一般也将其配置到PATH环境变量中。同时,在配置java环境的同事,不能影响其它环境的运行。所以,以windows下面环境变量的配置(各个目录之间用;隔开)为例,通常将下面的内容加到PATH环境变量的最左侧:

%JAVA_HOME%/bin/;%JAVA_HOME%/jre/bin

2、CLASSPATH环境变量

2.1 作用简介

和PATH变量不同,CLASSPATH环境变量的作用是指定Java类所在的目录(或许它的意思就是PATH of Class)。
当运行java程序的时候,要指定相应的类名,比如,下面的例子中,在C:\test\目录下写一个HelloWorld,并执行:

c:\test>type HelloWorld.java   #查看文本文件的内容
public class HelloWorld{
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!!");
}
}
c:\test>javac HelloWorld.java #因为配置了PATH环境变量,在任意目录下都可执行javac c:\test>dir #查看编译生成的class文件
2016/03/28 22:13 427 HelloWorld.class
2016/03/28 22:08 152 HelloWorld.java
c:\test>java HelloWorld #运行HelloWorld(注意,不能加.class后缀)
错误: 找不到或无法加载主类 HelloWorld

这里报错找不到或无法加载主类 HelloWorld,前面说到

CLASSPATH环境变量的作用是指定Java类所在的目录。

下面看下此时环境中CLASSPATH环境变量的值是什么:

c:\test>echo %CLASSPATH%
C:\Program Files\Java\jdk1.8.0_51\lib\tools.jar;C:\Program Files\Java\jdk1.8.0_51\lib\dt.jar
c:\test>

果真,没有HelloWorld.class所在的目录。下面我们通过手动指定CLASSPATH解决该问题:

c:\test>java -classpath . HelloWorld
Hello World!!
c:\test>

实际上,和PATH环境变量也是由左到右搜索的,所以,在向CLASSPATH中添加新的目录时,通常将其放在最左侧。下面的例子中,在指定-classpath选项的参数时,引用了%CLASSPATH%环境变量:

c:\test>java -classpath .;%CLASSPATH% HelloWorld
错误: 找不到或无法加载主类 Files\Java\jdk1.8.0_51\lib\tools.jar;C:\Program
c:\test>java -classpath ".;%CLASSPATH%" HelloWorld
Hello World!!
c:\test>echo ".;%CLASSPATH%"
".;C:\Program Files\Java\jdk1.8.0_51\lib\tools.jar;C:\Program Files\Java\jdk1.8.0_51\lib\dt.jar"

ps:如果刚装完JDK,没有配置环境变量,那么缺省的%CLASSPATH%环境变量的值是.,也就是当前目录。

2.2 通常如何配置

Java中通常将环境变量CLASSPATH配置为.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar。其中为何包含.,在上面的例子中已经看得很清楚了。下面的内容大概介绍了另外两个的作用:

dt.jar:运行环境类库,主要是Swing包,这一点通过用压缩软件打开dt.jar也可以看到。如果在开发时候没有用到Swing包,那么可以不用将dt.jar添加到CLASSPATH变量中。

tools.jar:工具类库,它跟我们程序中用到的基础类库没有关系。我们注意到在Path中变量值bin目录下的各个exe工具的大小都很小,一般都在27KB左右,这是因为它们实际上仅仅相当于是一层代码的包装,这些工具的实现所要用到的类库都在tools.jar中,用压缩软件打开tools.jar,你会发现有很多文件是和bin目录下的exe工具相对性的,如下图:

From:The use of CLASSPATH

 

2.3 指定CLASSPATH的注意事项

Class Path Wild Cards

Class path entries can contain the base name wildcard character (), which is considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR. For example, the class path entry mydir/ specifies all JAR files in the directory named mydir. A class path entry consisting of * expands to a list of all the jar files in the current directory. Files are considered regardless of whether they are hidden (have names beginning with '.').
A class path entry that contains an asterisk () does not match class files. To match both classes and JAR files in a single directory mydir, use either mydir:mydir/ or mydir/:mydir. The order chosen determines whether the classes and resources in mydir are loaded before JAR files in mydir or vice versa.
Subdirectories are not searched recursively. For example, mydir/
searches for JAR files only in mydir, not in mydir/subdir1, mydir/subdir2, and so on.
The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required, then the JAR files can be enumerated explicitly in the class path.
Expansion of wild cards is done early, before the invocation of a program's main method, rather than late, during the class-loading process. Each element of the input class path that contains a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory mydir contains a.jar, b.jar, and c.jar, then the class path mydir/* is expanded into mydir/a.jar:mydir/b.jar:mydir/c.jar, and that string would be the value of the system property java.class.path.
The CLASSPATH environment variable is not treated any differently from the -classpath or -cp options. Wild cards are honored in all of these cases. However, class path wild cards are not honored in the Class-Path jar-manifest header.

Folders and Archive Files

When classes are stored in a directory (folder), such as c:\java\MyClasses\utility\myapp, then the class path entry points to the directory that contains the first element of the package name (in this case, C:\java\MyClasses, because the package name is utility.myapp).
When classes are stored in an archive file (a zip or JAR file) the class path entry is the path to and including the zip or JAR file. For example, the command to use a class library that is in a JAR file as follows:

java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool

Multiple Specifications

To find class files in the directory C:\java\MyClasses and classes in C:\java\OtherClasses, you would set the class path to the following. Note that the two paths are separated by a semicolon.

java -classpath C:\java\MyClasses;C:\java\OtherClasses ...

Specification Order

The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the previous example, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses. Only when it does not find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.

From: docs.oracle.com

3、JAVA_HOME环境变量

这个是不重要的,题目中我都没有提它。它唯一的作用就是,前面两个环境变量的配置中引用了它,所以,要将其配置为:

C:\Program Files\Java\jdk1.8.0_51\

如果前面环境变量的配置都显式指定了完成的路径,那么完全可以不用配置JAVA_HOME环境变量。

转载自:https://www.jianshu.com/p/d63b099cf283

Java环境变量PATH和CLASSPATH的更多相关文章

  1. java环境变量 Path 与CLASSPATH

    1.Windows操作系统根据Path环境变量来查找命令,Linux操作系统则根据PATH环境变量来查找命令 因为Windows操作系统不区分大小写,设置Path和PATH并没有区别,而Linux系统 ...

  2. Java入门:Java环境变量PATH、CLASSPATH、JAVA_HOME

    一些初学者在用java HelloWorld指令运行程序的时候出现: Exception in thread "main" java.lang.NoClassDefFoundErr ...

  3. [转]JAVA环境变量JAVA_HOME、CLASSPATH、PATH设置详解

    [转] JAVA环境变量JAVA_HOME.CLASSPATH.PATH设置详解 - dreamman的日志 - 网易博客http://blog.163.com/dreamman_yx/blog/st ...

  4. JAVA环境变量JAVA_HOME、CLASSPATH、PATH设置详解

    Windows下JAVA用到的环境变量主要有3个,JAVA_HOME.CLASSPATH.PATH.下面逐个分析. JAVA_HOME 指向的是JDK的安装路径,如C:\jdk1.5.0_06,在这路 ...

  5. Java中环境变量PATH与CLASSPATH的区别

    在安装JDK时需要添加环境变量,经常使用的环境变量有两个: PATH与CLASSPATH 下面总结一下环境变量的作用. PATH是系统用来指定可执行文件的完整路径.当在CMD中执行命令时,如果执行的可 ...

  6. WINDOWS系统下环境变量PATH和CLASSPATH的意思

    1 PATH 对于没有包含路径的命令,WINDOWS系统会默认去Windows 目录(C:\windows)和系统目录(C:\windows\system32)查找,如果没有找到,就去PATH变量内包 ...

  7. Java 环境变量设置 -- JAVA_HOME CLASSPATH

    1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.7.0 ...

  8. Windows 下java环境变量的配置(Windows7 ,8,8.1,10)

    Windows 下java环境变量的配置 在“系统”面板的左上角选择“高级系统设置”,在弹出的系统属性中选择”高级“项,然后点击右下角的“环境变量(N)...”,就此进入JAVA环境变量的配置. 如果 ...

  9. JAVA环境变量配置详解

    JAVA环境变量JAVA_HOME.CLASSPATH.PATH设置详解 Windows下JAVA用到的环境变量主要有3个,JAVA_HOME.CLASSPATH.PATH. JAVA_HOME 指向 ...

随机推荐

  1. Java 动态绑定

    转载  http://www.cnblogs.com/ygj0930/p/6554103.html 一:绑定 把一个方法与其所在的类/对象 关联起来叫做方法的绑定.绑定分为静态绑定(前期绑定)和动态绑 ...

  2. 写好的Java代码在命令窗口运行——总结

    步骤: 1.快捷键 win+r,在窗口中输入cmd,enter键进入DOS窗口. 2.假设写好的代码的目录为:D:\ACM 在DOS中依次写入:cd d: cd ACM 利用cd切换到代码文件所在的目 ...

  3. 针对不同.NET版本的条件编译

    原理:查找项目目录下的 csproj 文件,解析它,找到节点TargetFrameworkVersion,判断.net版本

  4. <ROS> message_filters 对齐多种传感器数据的时间戳

    联合标定三维雷达和IMU,第一步要先对齐两种传感信息的时间戳. ros官网提供了message_filters用于对齐多种传感信息的时间戳. http://wiki.ros.org/message_f ...

  5. H264--2--语法及结构

    转自:http://blog.csdn.net/yangzhongxuan/article/details/8003494 名词解释 场和帧 :    视频的一场或一帧可用来产生一个编码图像.在电视中 ...

  6. windows下的拷贝利器robocopy

    robocopy xxcopy http://www.cnblogs.com/zhanglei1371/p/6724167.html [转载]robocopy的用法 经常进行文件管理操作的朋友们,不满 ...

  7. @EnableEurekaServer无法正常import原因是spring-cloud-dependencies版本太低

    去maven repository 库里 http://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-depend ...

  8. 20175236 2018-2019-2 《Java程序设计》第七周学习总结

    教材学习内容总结 String类 String类在java.lang包中的类被默认引入,因此程序可以直接使用String类 Java把String类定义为final类,因此用户不能扩展String类, ...

  9. JeeCMS v7 SSRF导致任意文件写入

    前言: 学习大佬的思路. from先知社区:https://xz.aliyun.com/t/4809 00X1: 是/ueditor/getRemoteImage.jspx 接口 通过构造upfile ...

  10. mvcmovie sample 在window10 下的部署问题(HTTP Error 500.19 - Internal Server Error)

    mvcmovie sample 在window10 下的部署问题 使用VS2018配置好了mvcmovie sample,发布到IIS后,打开报错: HTTP Error 500.19 - Inter ...