`

[转]PATH and CLASSPATH

    博客分类:
  • JAVA
 
阅读更多

PATH and CLASSPATH

This section explains how to use the PATH and CLASSPATH environment variables on Microsoft Windows, Solaris, and Linux. Consult the installation instructions included with your installation of the Java Development Kit (JDK) software bundle for current information.

After installing the software, the JDK directory will have the structure shown below.

JDK directory structure

The bin directory contains both the compiler and the launcher.

Update the PATH Environment Variable (Microsoft Windows)

You can run Java applications just fine without setting the PATH environment variable. Or, you can optionally set it as a convenience.

Set the PATH environment variable if you want to be able to conveniently run the executables (javac.exejava.exe,javadoc.exe, and so on) from any directory without having to type the full path of the command. If you do not set the PATHvariable, you need to specify the full path to the executable every time you run it, such as:

C:\Java\jdk1.7.0\bin\javac MyClass.java

The PATH environment variable is a series of directories separated by semicolons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should have only one bin directory for the JDK in the path at a time (those following the first are ignored), so if one is already present, you can update that particular entry.

The following is an example of a PATH environment variable:

C:\Java\jdk1.7.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem

It is useful to set the PATH environment variable permanently so it will persist after rebooting. To make a permanent change to the PATH variable, use the System icon in the Control Panel. The precise procedure varies depending on the version of Windows:

Windows XP
  1. Select Start, select Control Panel. double click System, and select the Advanced tab.
  2. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  3. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Windows Vista:
  1. From the desktop, right click the My Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced tab (Advanced system settings link in Vista).
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Windows 7:
  1. From the desktop, right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

Note: You may see a PATH environment variable similar to the following when editing it from the Control Panel:
%JAVA_HOME%\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
Variables enclosed in percentage signs (%) are existing environment variables. If one of these variables is listed in the Environment Variables window from the Control Panel (such as JAVA_HOME), then you can edit its value. If it does not appear, then it is a special environment variable that the operating system has defined. For example,SystemRoot is the location of the Microsoft Windows system folder. To obtain the value of a environment variable, enter the following at a command prompt. (This example obtains the value of the SystemRoot environment variable):
echo %SystemRoot%

Update the PATH Variable (Solaris and Linux)

You can run the JDK just fine without setting the PATH variable, or you can optionally set it as a convenience. However, you should set the path variable if you want to be able to run the executables (javacjavajavadoc, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

% /usr/local/jdk1.7.0/bin/javac MyClass.java

To find out if the path is properly set, execute:

% java -version

This will print the version of the java tool, if it can find it. If the version is old or you get the error java: Command not found, then the path is not properly set.

To set the path permanently, set the path in your startup file.

For C shell (csh), edit the startup file (~/.cshrc):

set path=(/usr/local/jdk1.7.0/bin )

For bash, edit the startup file (~/.bashrc):

PATH=/usr/local/jdk1.7.0/bin:
export PATH

For ksh, the startup file is named by the environment variable, ENV. To set the path:

PATH=/usr/local/jdk1.7.0/bin:
export PATH

For sh, edit the profile file (~/.profile):

PATH=/usr/local/jdk1.7.0/bin:
export PATH

Then load the startup file and verify that the path is set by repeating the java command:

For C shell (csh):

% source ~/.cshrc
% java -version

For kshbash, or sh:

% . /.profile
% java -version

Checking the CLASSPATH variable (All platforms)

The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be defined through other means, such as the bootstrap class path or the extensions directory.)

The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care.

The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.

To check whether CLASSPATH is set on Microsoft Windows NT/2000/XP, execute the following:

C:> echo %CLASSPATH%

On Solaris or Linux, execute the following:

% echo $CLASSPATH

If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) or simply %CLASSPATH%(Microsoft Windows NT/2000/XP).

To modify the CLASSPATH, use the same procedure you used for the PATH variable.

Class path wildcards allow you to include an entire directory of .jar files in the class path without explicitly naming them individually. For more information, including an explanation of class path wildcards, and a detailed description on how to clean up the CLASSPATH environment variable, see the Setting the Class Path technical note.

 

注:本文转自ORACLE官网:http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

 

分享到:
评论

相关推荐

    解决The project cannot be built until build path

    导致“项目无法构建直到build path”问题的原因有多种,包括项目的 Build Path 配置错误、jar 包依赖冲突、 CLASSPATH 变量设置不正确等。在解决该问题前,需要先了解 Eclipse 的 Build Path 配置机制。 Build Path...

    Cross –compile GNU Classpath 0.98 and jamvm 1.54 to ARM 9

    /jamvm_arm/bin/jamvm -cp <your_class_path> ``` 以上步骤完成后,你应该能够在 ARM 9 设备上成功运行由 jamvm 支持的 Java 应用了。不过,这个过程中可能还会遇到其他依赖问题或编译错误,需要根据实际情况...

    appfuse.tar.gz

    To begin, download Maven 2.0.9, install it, and add $M2_HOME/bin to your $PATH. Create a MAVEN_OPTS environment variable and set its value to: -Xms512M -Xmx512M All issues should be logged in JIRA ...

    Tomcat源码开发环境搭建

    Use Windows->Preferences and then Java->Build Path->Classpath Variables to add two new Classpath variables: TOMCAT_LIBS_BASE 路径指向解压后的baseconfig ANT_HOME 路径指向解压后的apache-ant-1.8.4 2、...

    Springboot访问Resource下的文件.docx

    - 使用 `ClassPathResource` 类可以方便地从 classpath 中加载资源。 - 示例代码: ```java ClassPathResource resource = new ClassPathResource("file.txt"); InputStream inputStream = resource....

    Generate the JUNIT report by Ant and Junit

    <path id="classpath"> **/*.jar"/> <pathelement location="src"/> </path> <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="classpath"/> <classpath refid=...

    WIN7下JDK1.8环境变量配置图解详细过程[归纳].pdf

    - Oracle官方文档: [Setting the PATH and CLASSPATH Variables](https://docs.oracle.com/javase/8/docs/technotes/guides/install/windows_jdk_install.html#A1079667) - Java官方教程: [Setting Up Your ...

    android studio 3.6.1导入项目报错提示无法下载classpath里的内容

    Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all artifacts for configuration ‘:classpath’. 原因: 百度说是无法下载...

    jdk8-window-and-linux.zip

    安装完成后,JDK会在系统路径中添加必要的环境变量,包括`JAVA_HOME`、`PATH`和`CLASSPATH`,使得在命令行中可以轻松运行Java相关命令,如`javac`(编译器)和`java`(解释器)。 接下来是Linux版本的JDK,文件名为...

    Linux版本jdk8u-211 对应jdk1.8.0_211版本

    3. **配置环境变量**:为了使得系统能够识别和使用新安装的JDK,需要设置`JAVA_HOME`、`PATH`和`CLASSPATH`环境变量。可以在用户的`.bashrc`或`.bash_profile`文件中添加如下行: ``` export JAVA_HOME=/path/to/...

    jdk1.7_80 window.64

    - 安装JDK 1.7_80 by following the setup wizard, ensuring to choose the appropriate installation directory and configuring the environment variables, such as JAVA_HOME, PATH, and CLASSPATH. - Verify ...

    java命令行编译说明

    java -classpath <path_to_classes_and_libs> edu.main.A ``` 其中`<path_to_libs>`和`<path_to_classes_and_libs>`应替换为实际的目录路径或包含库的jar文件路径。 #### 小结 在Java命令行编译和运行过程中,...

    oracle10g完全卸载说明

    删除环境变量中的 PATH 和 CLASSPATH 中包含 Oracle 的值,以免影响系统的运行。 Step 8: 删除“开始—程序”中所有 Oracle 的组和图标 删除“开始—程序”中所有 Oracle 的组和图标,以免留下残余的组件。 Step ...

    android studio 3.6.1导入项目报错(无法下载classpath里的内容)

    Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all artifacts for configuration ‘:classpath’. 原因: 百度说是无法下载...

    sigar-amd64-winnt.rar

    标题“sigar-amd64-winnt.rar”指的是一个针对Windows NT操作系统的64位版本的SIGAR(System Information Gatherer and Reporter)库的压缩文件。SIGAR是一个跨平台的工具集,它允许应用程序获取系统级别的信息,如...

    如何搭建Android的开发环境.pdf

    安装完成后,你需要设置三个系统变量:JAVA_HOME、PATH和CLASSPATH。JAVA_HOME指向JDK的安装路径,PATH使得系统能在任意目录下识别java命令,而CLASSPATH则是Java加载类库的路径。确保这些环境变量设置正确,可以...

    and 常用命令

    4. **添加 PATH**:在系统环境变量 `PATH` 中加入 Ant 的 `bin` 目录路径,例如 `E:\apache-ant-1.7.0\bin`,这样可以在任意目录下调用 Ant 命令。 **测试 Ant 是否安装成功**: - 进入 Ant 的 `bin` 目录,执行...

    java 操作jacob 生成word,html或者excel

    jacob.jar: a JAR file for the java classes which you must add to your CLASSPATH. The package names replace com.ms with com.jacob (for example com.ms.com.Variant maps to com.jacob.com.Variant.  jacob...

    java-jacob 打印

    jacob.jar: a JAR file for the java classes which you must add to your CLASSPATH. The package names replace com.ms with com.jacob (for example com.ms.com.Variant maps to com.jacob.com.Variant. jacob....

Global site tag (gtag.js) - Google Analytics