1. 程式人生 > >java -cp用法

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檔案記錄了可用的類~