# java命令行下如何设置classpath类路径 转

猪刚烈

## Setting the path to execute Java programs

### Basic usage

Suppose we have a package called org.mypackage containing the classes:

• HelloWorld (main class)
• SupportClass
• UtilClass

and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).

The file structure will look like this:

Microsoft Windows Linux
``````D:\myprogram\
|
---> org\
|
---> mypackage\
|
---> HelloWorld.class
---> SupportClass.class
---> UtilClass.class``````
``````/home/user/myprogram/
|
---> org/
|
---> mypackage/
|
---> HelloWorld.class
---> SupportClass.class
---> UtilClass.class``````

When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we use the following command:

Microsoft Windows Linux
``java -classpath D:\myprogram org.mypackage.HelloWorld``
``java -classpath /home/user/myprogram org.mypackage.HelloWorld``

where:

• -classpath D:\myprogram sets the path to the packages used in the program (on Linux, -classpath /home/user/myprogram)
• org.mypackage.HelloWorld is the name of the main class

Note that if we ran Java in D:\myprogram\ (on Linux, /home/user/myprogram/) then we would not need to specify the classpath since Java implicitly looks in the current working directory for files containing classes.

### Adding all JAR files in a directory

In Java 6 and higher, one can add all jar-files in a specific directory to the classpath using wildcard notation.

Windows example:

``java -classpath ".;c:\mylib\*" MyApp``

Linux example:

``java -classpath '.:/mylib/*' MyApp``

### Setting the path through an environment variable

The environment variable named CLASSPATH may be alternatively used to set the classpath. For the above example, we could also use on Windows:

Sometimes you have to check the JAVA_HOME also, if it is pointing towards the right JDK version

``````set CLASSPATH=D:\myprogram
java org.mypackage.HelloWorld``````

### Setting the path of a Jar file

Now, suppose the program uses a supporting library enclosed in a Jar file called supportLib.jar, physically in the directory D:\myprogram\lib\.

The corresponding physical file structure is :

``````D:\myprogram\
|
---> lib\
|
---> supportLib.jar
|
---> org\
|
--> mypackage\
|
---> HelloWorld.class
---> SupportClass.class
---> UtilClass.class``````

We should use the following command-line option:

``java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld``

or alternatively:

``````set CLASSPATH=D:\myprogram;D:\myprogram\lib\supportLib.jar
java org.mypackage.HelloWorld``````

### Setting the path in a Manifest file

Suppose that our program has been enclosed in a Jar file called helloWorld.jar, put directly in the D:\myprogram directory. We have the following file structure:

``````D:\myprogram\
|
---> helloWorld.jar
|
---> lib\
|
---> supportLib.jar``````

The manifest file defined in this Jar file has this definition:

``````Main-Class: org.mypackage.HelloWorld
Class-Path: lib/supportLib.jar``````

Note: It's important that the manifest file ends with either a new line or carriage return.

Also, note that the classpath string in this case describes the location of the supportLib.jar file relative to the location of the helloWorld.jar file, and not as an absolute file path (as it might be when setting the -classpath parameter on the command line, for example). Thus, the actual locations of the jar file and its support library are irrelevant so long as the relative directory structure between the two is preserved.

To launch the program, we can use the following command:

``java -jar D:\myprogram\helloWorld.jar``

It is not necessary to define the Classpath to the program classes, or the support library classes, because it is already defined in the manifest file.

Caution, it is useless to define the Main class at launch, the manifest of the JAR file must contain a line of the form

``Main-Class: classname``

in order for the -jar option to work JavaDoc.

The syntax for specifying multiple library JAR files in the manifest file is to separate the entries with a space:

``Class-Path: lib/supportLib.jar lib/supportLib2.jar``

## OS specific notes

Being closely associated with the file system, the command-line Classpath syntax depends on the operating system. For example:

• on all Unix-like operating systems (such as Linux and Mac OS X), the directory structure has a Unix syntax, with separate file paths separated by a colon (":").
• on Windows, the directory structure has a Windows syntax, and each file path must be separated by a semicolon (";").

This does not apply when the Classpath is defined in manifest files, where each file path must be separated by a space (" "), regardless of the operating system.

## Diagnose

Application programmers may want to find out/debug the current settings under which the application is running:

``System.getProperty("java.class.path")``

### 猪刚烈

win10下java的jdk安装和环境变量配置

codingcoge ⋅ 03/09 ⋅ 0

Linux 下shell启动Java Main函数脚本

#!/bin/sh # #该脚本为Linux下启动java程序的通用脚本。即可以作为开机自启动service脚本被调用， #也可以作为启动java程序的独立脚本来使用。 #警告!!!：该脚本stop部分使用系统kill命令来强...

【Java学习】Win10下最新Java环境配置

【背景】 最近小编开始学习Java知识了，所以需要安装JDK，配置java环境，但小编学习的资料里的配置教程是xp系统的，与现编使用的Win10系统配置步骤不一样，所以小编觉得需要记录一下，Win10...

yym15732626210 ⋅ 03/24 ⋅ 0

ubuntu java java.jar main.class问题

/etc/profile 设置: export JAVAHOME=/usr/local/java/jdk1.8.0161 export JREHOME=\$JAVAHOME/jre export CLASSPATH=.:\$JAVAHOME/lib:\$JREHOME/lib:\$CLASSPATH export PATH=\$JAVAHOME/bin:\$......

luxury515 ⋅ 04/30 ⋅ 0

2018年java编程语言经典基础知识总结学习

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性，广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互...

Java小辰 ⋅ 05/21 ⋅ 0

Groovy轻松入门——搭建Groovy开发环境

thinkyoung ⋅ 2014/10/26 ⋅ 0

Java基础|Java特性与HelloWorld运行流程

【Java基础】 Java语言特点：（着重了解两个）开源、跨平台。 跨平台如何实现：通过JVM实现，JVM充当Java和不同OS之间的翻译器，不同OS对应不同JVM。 Java语言的平台：JavaSE、JavaME(Androi...

darlingwood2013 ⋅ 05/29 ⋅ 0

Qt Jambi 4.8 快速入门

xiaomin0322 ⋅ 06/12 ⋅ 0

Java环境变量的配置 （Win10环境下）

ling118 ⋅ 05/08 ⋅ 0

Windows server 2008和centos 7安装jdk

Windows server 2008和centos 7安装jdk JDK JDK是 Java 语言的软件开发工具包，主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心，它包含了JAVA的运行环境（JVM+Jav...

luchao669 ⋅ 05/31 ⋅ 0

JerryWang_SAP ⋅ 28分钟前 ⋅ 0

Cube的构建过程

Cube 的构建方式有两种：全量构建和增量构建。两者的构建过程完全一样，区别在于构建时读取的数据源是全集还是子集。 Cube的构建步骤： 1.创建临时的 Hive 平表（从 Hive 读取数据）。 2.计算...

637. Average of Levels in Binary Tree - LeetCode

Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这个树,把map里面填值,遍历结束后,再遍历这个map,把每...

yysue ⋅ 50分钟前 ⋅ 0

IDEA配置和使用

bithup ⋅ 今天 ⋅ 0

Hive函数

1.函数explode （一转多） create table hive_wc(sentence string); load data local inpath '/home/hadoop/data/hive-wc.txt' into table hive_wc; 结果： hive > select * from hive_wc; ......

GordonNemo ⋅ 今天 ⋅ 0

PE格式第三讲扩展,VA,RVA,FA的概念

simpower ⋅ 今天 ⋅ 0

180623-SpringBoot之logback配置文件

SpringBoot配置logback 项目的日志配置属于比较常见的case了，之前接触和使用的都是Spring结合xml的方式，引入几个依赖，然后写个 logback.xml 配置文件即可，那么在SpringBoot中可以怎么做？...

Vagrant setup