1. 程式人生 > >你知道怎麼執行java嗎

你知道怎麼執行java嗎

1. runnable jar

選擇主類後打成jar包

MANIFEST.MF是這樣的

Manifest-Version: 1.0
Class-Path: .
Main-Class: com.dic.helloworld.Main

執行 直接 java -jar   jar包名就可以

2.  -cp  -classpath 這種方式

今天由於自己學藝不精 被坑的非常嚴重

-classpath  與-cp 一樣,將jar包配上即可,如 -cp ./lib/*.jar 怎麼樣,這樣這樣是不可以的,classpath jar包和jar包之間有個東西還記得嗎? linux下為  :  windows下為 ;

這是什麼呢,看文件 永遠都是最好的選擇

 /**
     * The system-dependent path-separator character.  This field is
     * initialized to contain the first character of the value of the system
     * property <code>path.separator</code>.  This character is used to
     * separate filenames in a sequence of files given as a <em>path list</em>.
     * On UNIX systems, this character is <code>':'</code>; on Microsoft Windows systems it
     * is <code>';'</code>.
     *
     * @see     java.lang.System#getProperty(java.lang.String)
     */
    public static final char pathSeparatorChar = fs.getPathSeparator();

所以如果一個目錄下你有很多jar包要載入 而且你又要是用-cp這種方式,那麼問題來了,該怎麼寫

LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"`

我只知道linux環境下怎麼寫哦,把這個目錄下的jar檔案全部放在classpath而且兩個jar之間必須是有   File.pathSeparatorChar才可以。這樣就可已運行了,

3.另外一種方式 就是  -Djava.ext.dirs=./lib/  只要指定jar檔案的路徑就可已

然後看一下classloader 


classLoader的問題以後深揪,今天也算是長見識了。