文档章节

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

猪刚烈
 猪刚烈
发布于 2014/10/12 11:40
字数 867
阅读 13
收藏 0
点赞 0
评论 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")

本文转载自:http://blog.csdn.net/bluishglc/article/details/9972951

共有 人打赏支持
猪刚烈
粉丝 22
博文 708
码字总数 110
作品 1
海淀
程序员
win10下java的jdk安装和环境变量配置

首先需要去JAVA官网下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html 下载的是jdk 9 与以往的环境配置不一样 jdk是java的核心 是java软件的开...

codingcoge ⋅ 03/09 ⋅ 0

Linux 下shell启动Java Main函数脚本

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

心月狐 ⋅ 05/14 ⋅ 0

【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开发环境

多日来,我发表了多篇关于Groovy的文章,想必大家对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 快速入门

因为一个项目几个库均是jar库,而本人对于Qt 很熟悉.因此采用Qt Jambi 进行开发 这里用即用java 编写Qt 风格类库,而且还能用Qt Designer 进行非常对我胃口 几个常用链接 官方下载地址 http:/...

xiaomin0322 ⋅ 06/12 ⋅ 0

Java环境变量的配置 (Win10环境下)

一、已安装jdk 的win10计算机 1、首先找到‘此电脑’,右击选择属性 2、选择高级系统设置 3、选择环境变量 4、选择新建,变量名为 (JAVAHOME) 变量值为是之前安装jdk的路径,我的是安装在E:P...

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

没有更多内容

加载失败,请刷新页面

加载更多

下一页

如何将S/4HANA系统存储的图片文件用Java程序保存到本地

我在S/4HANA的事务码MM02里为Material维护图片文件作为附件: 通过如下简单的ABAP代码即可将图片文件的二进制内容读取出来: REPORT zgos_api.DATA ls_appl_object TYPE gos_s_obj.DA...

JerryWang_SAP ⋅ 28分钟前 ⋅ 0

Cube的构建过程

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

无精疯 ⋅ 32分钟前 ⋅ 0

云计算的选择悖论如何对待?

导读 人们都希望在工作和生活中有所选择。但心理学家的调查研究表明,在多种选项中进行选择并不一定会使人们更快乐,甚至不会产生更好的决策。心理学家Barry Schwartz称之为“选择悖论”。云...

问题终结者 ⋅ 36分钟前 ⋅ 0

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配置和使用

版本控制 svn IDEA版本控制工具不能使用 VCS-->Enable Version Control Integration File-->Settings-->Plugins 搜索Subversion,勾选SVN和Git插件 删除.idea文件夹重新生成项目 安装SVN客户......

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的概念

作者:IBinary 出处:http://www.cnblogs.com/iBinary/ 版权所有,欢迎保留原文链接进行转载:) 一丶VA概念 VA (virtual Address) 虚拟地址的意思 ,比如随便打开一个PE,找下它的虚拟地址 这边...

simpower ⋅ 今天 ⋅ 0

180623-SpringBoot之logback配置文件

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

小灰灰Blog ⋅ 今天 ⋅ 0

冒泡排序

原理:比较两个相邻的元素,将值大的元素交换至右端。 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第...

人觉非常君 ⋅ 今天 ⋅ 0

Vagrant setup

安装软件 brew cask install virtualboxbrew cask install vagrant 创建project mkdir -p mst/vmcd mst/vmvagrant init hashicorp/precise64vagrant up hashicorp/precise64是一个box......

遥借东风 ⋅ 今天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部