文档章节

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

猪刚烈
 猪刚烈
发布于 2014/10/12 11:40
字数 867
阅读 33
收藏 0

「深度学习福利」大神带你进阶工程师,立即查看>>>

以往基于构建工具和IDE工作,很少使用命令行执行程序,但偶尔使用命令行的时候关于类路径的设置都要查阅一下说明文档,这里找到一个详尽清晰的说明,以备自查.特别指明的是:classpath最好使用""(window系统下)或''(linux系统下)括起来,否则可能会因为包含的jar路径上含有空格一类的特殊字符导致出现奇怪的错误提示.,本文转自wikipedia,地址: http://en.wikipedia.org/wiki/Classpath_%28Java%29

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")
猪刚烈

猪刚烈

粉丝 22
博文 708
码字总数 110
作品 1
海淀
程序员
私信 提问
加载中
请先登录后再评论。
Netty那点事(三)Channel与Pipeline

Channel是理解和使用Netty的核心。Channel的涉及内容较多,这里我使用由浅入深的介绍方法。在这篇文章中,我们主要介绍Channel部分中Pipeline实现机制。为了避免枯燥,借用一下《盗梦空间》的...

黄亿华
2013/11/24
2W
22
访问安全控制解决方案

本文是《轻量级 Java Web 框架架构设计》的系列博文。 今天想和大家简单的分享一下,在 Smart 中是如何做到访问安全控制的。也就是说,当没有登录或 Session 过期时所做的操作,会自动退回到...

黄勇
2013/11/03
3.6K
8
Flappy Bird(安卓版)逆向分析(一)

更改每过一关的增长分数 反编译的步骤就不介绍了,我们直接来看反编译得到的文件夹 方法1:在smali目录下,我们看到org/andengine/,可以知晓游戏是由andengine引擎开发的。打开/res/raw/at...

enimey
2014/03/04
6.1K
18
浅入浅出Android(003):使用TextView类构造文本控件

基础: TextView是无法供编辑的。 当我们新建一个项目MyTextView时候,默认的布局(/res/layout/activity_main.xml)中已经有了一个TextView: <TextView 运行效果如下: 修改其文本内容...

樂天
2014/03/22
706
1
CDH5: 使用parcels配置lzo

一、Parcel 部署步骤 1 下载: 首先需要下载 Parcel。下载完成后,Parcel 将驻留在 Cloudera Manager 主机的本地目录中。 2 分配: Parcel 下载后,将分配到群集中的所有主机上并解压缩。 3 激...

cloud-coder
2014/07/01
6.9K
1

没有更多内容

加载失败,请刷新页面

加载更多

使用旁路输出(side output)来拆分和复制流

  我们在处理数据的时候,有时候想对不同情况的数据进行不同的处理,那么就需要把流进行拆分或者复制。 如果是使用filter来进行拆分,也能满足我们的需求,但每次筛选都要保留整个流,然后...

osc_ct0tt1cu
今天
18
0
Azure AD 与 AWS IAM 集成实现SSO—上(Azure部分)

整体的架构和流程是下面这个样子: Azure部分: 登录Azure的portal通过Azure Active Directory创建一个测试用户: 返回Azure Active Directory 创建新的应用程序 Amazon Web Service 搜索 Am...

osc_bgs3qxk5
今天
0
0
Azure AD 与 AWS IAM 集成实现SSO—下(AWS部分)

再回顾一下架构,我们都要做什么,别乱: 看来在AWS上做的工作要多一些。 登录AWS控制台: 进入到IAM这个服务: 在配置提供程序中,选择SAML,提供商名称自定义,比如WAAD,将刚才下载的元数...

osc_ibuoui1c
今天
4
0
AWS DevOps 通过Config自动审计Security Group配置——上篇

这个实验的一个场景是,运维同事设计安全组Security Group的时候,打开了除了HTTP和HTTPS的入口访问权限。其他协议或端口如果打开,除了审计不通过的同时,会自动触发一个函数将它修改成我们...

osc_l330x9u1
今天
3
0
AWS DevOps 通过Config自动审计Security Group配置——下篇

再讲一下背景, 这个实验的一个场景是,运维同事设计安全组Security Group的时候,打开了除了HTTP和HTTPS的入口访问权限。其他协议或端口如果打开,除了审计不通过的同时,会自动触发一个函数...

osc_cudh2wh2
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部