java -cp用法
原文出处:http://blog.csdn.net/zhuying_linux/article/details/7714194。感谢作者的分享
java -cp classpath
Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by colons (:). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program cannot tell the difference between the two invocations).
For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to a A.jar:b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the
list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, will be simi-larly expanded. Any classpath wildcard expansion occurs before the Java virtual
machine is started -- no Java program will ever see unexpanded wildcards except by querying the environment. For example; by invoking System.getenv("CLASSPATH").
For more information on class paths, see Setting the Class Path.
查看文档得到以上信息,以下是查找到的资料:
-cp 参数后面是类路径,是指定给解释器到哪里找到你的.class文件,
写法:
java -cp .;myClass.jar packname.mainclassname
classpath中的jar文件能使用通配符,如果是多个jar文件,要一个一个地罗列出来,从某种意义上说jar文件也就是路径。
要指定各个JAR文件具体的存放路径,相同路径有多个可使用通配符
java -cp .;c:/classes/myClass.jar;d:/classes/*.jar packname.mainclassname
packname.mainclassname为包含main方法的完全限定类名,如果在classpath中有多个还有main方法的类,通过此命令可以方便选定程序的入口。
例如:
#!/bin/sh
cd /home/work/bvpn/aaa/bin
pwd
nohup /usr/java/jdk1.6.0_11/bin/java -cp .:../lib/log4j-1.2.15.jar:/usr/java/jdk1.6.0_11/lib/dt.jar:/usr/java/jdk1.6.0_11/lib/tools.jar:/usr/java/jdk1.6.0_11/lib/:/usr/java/jdk1.6.0_11/lib/jradius-client.jar:../lib/mysql-connector-java-5.1.12-bin.jar authAgent.AuthAgentMain
2>&1 &
下面是一些遇到的问题:
1.jar -cp lib/referenced.jar -jar myworks.jar会执行报错,原因如下:
我们使用-jar选项的话java.exe会忽略-cp,-classpath,以及环境变量CLASSPATH的参数。
解决方法一:
不要使用-jar选项,直接调用含有main方法的class文件,这样-cp,-classpath以及环境变量里的CLASSPATH指定的参数就都能使用到了。
java -classpath ./lib/junit.jar:. test/Test1
解决方法二:
继续使用-jar选项,但是在MAINFEST.MF文件中指定引用到jar文件.
Class-Path: myplace/myjar.jar myplace/other.jar jardir/
另外说明一点:这个问题可能有些人遇不到,因为Java的版本不同的原因,我在Sun的JDK和IBM 1.5的JDK都遇到了这个问题,但是对于 IBM 1.4的JDK却没有类似问题
2. 批量加载包的方法:
(一)java命令引入jar时可以-cp参数,但时-cp不能用通配符(多个jar时要一个个写,不能*.jar),通常的jar都在同一目录,且多于1个。前些日子刚刚发现一个参数-Djava.ext.dirs~
如:java -Djava.ext.dirs=lib MyClass 【同样的weblogic中就有这样的参数:-Dweblogic.ext.dirs=$BEA_HOME/patch_weblogic920/profiles/default/sysext_manifest_classpath】
(二)通过Unix shell设置CLASSPATH方式加载(shell实在是方便)
CLASSPATH=`find ../lib -name *.jar|xargs|sed "s/ /:/g"`
CLASSPATH=".:$CLASSPATH
(三)cp 参数后面是类路径,是指定给解释器到哪里找到你的.class文件。使用java -cp命令可以调用一个其他目录下的带有main函数的类。格式为java -cp class文件的路径,下面的例子采用的相对路径。
String commandLine = "java -cp "+ "..\\kwicByPipeServer\\bin KWIC";
Process p = Runtime.getRuntime().exec(commandLine);
这样就可以调用了。
PS:
$BEA_HOME/wlserver_10.3/server/lib/weblogic.jar的classcompatibilityinspector-file-0.xml文件记录了可用的类~
java -cp用法的更多相关文章
- java -cp 用法介绍
java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库,jar包之类,需要全路径到jar包,window上分号“;” 分隔,linux上是分号“:”分隔.不支持通 ...
- Java -cp 命令行引用多个jar包的简单写法(Windows、Linux
1.Windows下用法 在Windows上,可以使用 用法:java your-jar-lib-folder/* your-main-class your-jar-lib-folder为存放一堆ja ...
- java -cp
java -cp /home/hdp/log4jTest/log4j-1.2.17.jar:/home/hdp/log4jTest/testLog.jar:/home/hdp/log4jTest/co ...
- 【转】java list用法示例详解
转自:http://www.jb51.net/article/45660.htm java中可变数组的原理就是不断的创建新的数组,将原数组加到新的数组中,下文对java list用法做了详解. Lis ...
- Java List 用法代码分析 非常详细
Java中可变数组的原理就是不断的创建新的数组,将原数组加到新的数组中,下文对Java List用法做了详解. List:元素是有序的(怎么存的就怎么取出来,顺序不会乱),元素可以重复(角标1上有个3 ...
- Java split用法
Java split用法 java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separat ...
- Java Enum用法详解
Java Enum用法详解 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举 ...
- java -cp通配符
JDK6支持java -cp后面跟通配符'*',试了一下发现还是需要注意: 错误方式(Wrong way): java -cp /data/apps/lib/*.jar com.chinacache ...
- java -cp & java jar的区别
java -cp java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库和jar包,需要全路径到jar包,多个jar包之间连接符:window上分号“;”.Lin ...
随机推荐
- JavaScript时间处理插件
摘要:代码返回的有两种时间格式 一种是/// 另外一种是---分割的 两个接收参数的说明 timestr 是接收的时间 mark是格式 默认返回的格式是/// 加上- 返回的格式是- ...
- Unity3d 协程
参考文章: http://blog.csdn.net/onafioo/article/details/48979939 http://www.cnblogs.com/zhaoqingqing/p/37 ...
- 老版VC++线程池
在一般的设计中,当需要一个线程时,就创建一个,但是当线程过多时可能会影响系统的整体效率,这个性能的下降主要体现在:当线程过多时在线程间来回切换需要花费时间,而频繁的创建和销毁线程也需要花费额外的机器指 ...
- 魔方 NewLife.Cube
魔方 是一个基于 ASP.NET MVC 的 用户权限管理平台,可作为各种信息管理系统的基础框架. 演示:http://cube.newlifex.com 源码 演示账号:admin/admin 源码 ...
- iOS 开发 右滑返回上一级控制器
#import <objc/runtime.h> @interface UINavigationController (Transition)<UIGestureRecognizer ...
- 使用 requirejs 打包 jQuery 插件 datetimepicker 的问题记录
网站之前用的时间选择 UI 实在太丑,而且功能单一,决定全站改用 https://github.com/xdan/datetimepicker/ 里面有好几个 js,奇怪的是,只有 /build 目录 ...
- 【转载】wifi一键配网smartconfig原理及应用
物联网给我们又提供了一种窃取WiFi密码的好方式:让智能设备主动断线. 同时也提供一种让智能设备连接到恶意WiFi的方式:设备一键配置功能时疯狂广播恶意WiFi的信息. 转自:http://blog. ...
- JavaScript 数组对象的去重
JavaScript数组去重 1.原型去重法.通过prototype找到数组的源性对象Array,在数组的原型上添加unique()方法.需要使用的时候使用 点 " . " 进行连 ...
- python os 模块
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cdos.curdir ...
- 实例讲解基于 React+Redux 的前端开发流程
原文地址:https://segmentfault.com/a/1190000005356568 前言:在当下的前端界,react 和 redux 发展得如火如荼,react 在 github 的 s ...