在JDK安装好后,要设置两个变量Path和Classpath,Path是操作系统要求的,这里不谈了,而classpath是Java虚拟机要求的这里做一个详细的解释。

一、classpath的作用

==============

The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

二、classpath如何设置

==============

windows下的设置很简单。

右键“计算机”->属性->高级系统设置->系统属性对话框->高级选项卡->环境变量,在调出的环境变量对话框中有两种环境变量:1、用户变量,2、系统变量。同一个环境变量既可以在用户变量中设置也可以在系统变量中设置,比如Path环境变量。这两者的区别是,系统环境变量对系统中的每一个用户都有效,而用户环境变量只对当前用户有效。

具体来说就是在读取环境变量的时候,如果用户变量和系统变量中同时设置了同一个变量,则先读取在系统变量中设置的内容,然后再读取用户变量中设置的内容。

主要说一下linux下的设置。

linux下环境变量设置有两个地方。一个是在/etc/profile文件中,这个文件中设置的环境变量是系统级别的,相当于windows中的系统变量;另一个地方是~/.bash_profile,这个文件中设置的环境变量是用户级别的,相当于windows中的用户变量。

同一个变量在/etc/profile和~/.bash_profile中同时设置时,当echo  $variableName的时候先读取/etc/profile中的值然后读取~/.bash_profile中的值,最后组合成最终的输出。

三、classpath的内容

===============

classpath的内容就是哪些可以设置到classpath中去。

Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:

  • For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
  • For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
  • For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).

Multiple path entries are separated by semi-colons. With the set command, it's important to omit spaces from around the equals sign (=).

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

Classpath entries that are neither directories nor archives (.zip or .jar files) nor * are ignored.

四、其他问题

===============

classpath这个环境变量在设置的时候,应该写成大写还是写成小写呢?

这个问题在linux下没有疑问,因为linux下的环境变量约定都是大写的,而且linux下环境变量是区分大小写的,所以在linux下应该写成大写的。

但是这个问题在windows下是有区别的。因为windows下的环境变量不区分大小写,所以原则上写成大写或者小写都可以。一般情况下设置的时候还是推荐设置成大写的,原因是其他的软件比如ant、tomcat等是要求CLASSPATH为大写的,所以为了将来和这些软件良好的配合,最好还是写成大写的。

另外,关于Classpath设置,oracle的JDK团队有一个解释:http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html , 需要的时候可以详细参考。

Java的CLASSPATH的更多相关文章

  1. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

  2. java获取classpath文件路径空格转变成了转义字符%20的问题

    java获取classpath文件路径空格转变成了转义字符%20的问题 这个问题很纠结,服务器的文件路径带有空格,空格被转化是%20了,悲剧就出现了 下面展示一段代码String path = get ...

  3. java classpath import package 机制 @Java的ClassPath, Package和Jar

    java classpath import package 机制   從一個簡單的例子談談package與import機制 基本原則:為什麼需要將Java文件和類文件切實安置到其所歸屬之Package ...

  4. java中classPath和Xpath问题

    java中classPath和Xpath问题 今天遇到一个问题想获取classpath对应的目录,开始还以为java源代码可以像spring配置文件.xml中一样通过classpath:来获取对应的路 ...

  5. 谈谈Java的classpath

    Java之ClassPath 大家刚开始写Java代码的时候,如果使用Eclipse作为IDE,同时需要引用其他的类库,一般会有如下操作 在工程中新建lib目录 将jar包复制到lib目录下 右键单击 ...

  6. Java中classpath配置

    Java中classpath配置 一.DOS常用命令 二.DOS常用命令实例 2.1 转换目录 cd 1.6* 2.2 删除文件 del 删除文件(windows删除从里往外删) del *.txt ...

  7. 终于理解java的classpath!

    JAVA 的CLASSPATH 上面这样是可以的!!!!哇, 再也不会出现编译或是运行的时候,class 找不到的问题了.终于明白为什么了. java -cp  /ysr/my-app  P  这条命 ...

  8. Java的CLASSPATH,趁还没忘赶紧写点

    咳咳,睡眠不足加上年龄增长,真的赶脚记忆力不行啦. 接触Java以来,对于环境配置就是按照网上的教程,一路复制粘贴,也没啥想法; 最近决定啃啃ThinkInJava,没看两章就看到这CLASSPATH ...

  9. JAVA获取CLASSPATH路径

    ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL getResource (String name); public InputStream getResou ...

  10. java之classpath到底是什么

    如果你输入一个命令,比如java那么系统是如何找到这个命令的呢?按照顺序,系统先在当前目录搜索是否有java.exe, java.bat 等. 如果没有,就得到系统的PATH(不区分大小写)里面查找. ...

随机推荐

  1. TCP的连接和建立 图解

    前言 在没有理解TCP连接是如何建立和终止之前,我想你可能并不会使用connect,accept,close这三个函数并且使用netstat程序来调试应用.所以掌握TCP连接的建立和终止势在必行. 三 ...

  2. KeychainItemWrapper的使用

    KeychinaItemWrapper官方Demo下载地址KeychinaItemWrapper. NSString *identifier = @"xxxxxx";//你要使用的 ...

  3. 获取 metadata 过程详解 - 每天5分钟玩转 OpenStack(167)

    接上节,启动 neutron router 后 instance c1 终于拿到了 metadata, 从下面 c1 的启动日志可知: c1 所认为的 metadata 服务地址是 169.254.1 ...

  4. C#生成漂亮验证码完整代码类

    using System;using System.Web;using System.Drawing;using System.Security.Cryptography; namespace Dot ...

  5. Unity 3D Framework Designing(2)——使用中介者模式解耦ViewModel之间通信

    当你开发一个客户端应用程序的时候,往往一个单页会包含很多子模块,在不同的平台下,这些子模块又被叫成子View(视图),或者子Component(组件).越是复杂的页面,被切割出来的子模块就越多,子模块 ...

  6. 手动es6编译es5(命令行)

    package.json:"devDependencies": { "babel-cli": "^6.18.0", "babel- ...

  7. Open-Falcon 监控系统监控 MySQL/Redis/MongoDB 状态监控

    背景: Open-Falcon 是小米运维部开源的一款互联网企业级监控系统解决方案,具体的安装和使用说明请见官网:http://open-falcon.org/,是一款比较全的监控.而且提供各种API ...

  8. 老李分享: Oracle Performance Tuning Overview 翻译下

    1.2性能调优特性和工具 Effective data collection and analysis isessential for identifying and correcting perfo ...

  9. 细细探究MySQL Group Replicaiton — 配置维护故障处理全集

             本文主要描述 MySQL Group Replication的简易原理.搭建过程以及故障维护管理内容.由于是新技术,未在生产环境使用过,本文均是虚拟机测试,可能存在考虑不周跟思路有误 ...

  10. phpcms 笔记

      首先是要把首页分为三个部分  : 导航部分 .尾部和首页中间部分  用了三个不同的文件 header.html ; index.html; footer.html   在使用phpcms之前 首先 ...