java -cp

来源:互联网 发布:淘宝街拍风怎么拍 编辑:程序博客网 时间:2024/05/01 04:40

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文件记录了可用的类~