天天看點

java 執行JAR檔案總報 Could not find the main class

經過調查研究,執行JAR檔案時候JAVA會忽略所有的CLASSPATH設定,是以才會報這個錯誤,解決辦法

         1.把所有的依賴的庫一同打包到JAR檔案中,并且JAR檔案的MAIN-FEST檔案裡面定義CLASSPATH

         2.把JAR檔案放到CLASSPATH路徑中,執行JAVA   CLASS_NAME

詳細的原理和資料參考下面的内容:

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