Java的CLASSPATH

来源:互联网 发布:天下3 for mac 编辑:程序博客网 时间:2024/05/17 08:09

Java的CLASSPATH

在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 theCLASSPATH 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 , 需要的时候可以详细参考。

转自:http://www.cnblogs.com/zhangzl419/p/3482706.html

0 0