原文地址:http://www.java2000.net/viewthread.jsp?tid=6053
部落格地址:http://blog.csdn.net/java2000_net/archive/2008/06/08/2522408.aspx
1 今天徹底測試了jar程式
TestJar.java
-
Java code
-
package net.java2000.test.jar;import javax.swing.JOptionPane;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestJar { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } /** * @param args */ public static void main(String[] args) { BeanFactory beanFactory = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); TestJar obj = (TestJar) beanFactory.getBean("TestJar"); System.out.println(obj.getMessage()); JOptionPane.showMessageDialog(null, "Jar內容", "Jar標題", JOptionPane.OK_OPTION); }}
applicationContext.xml
-
XML code
-
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans <A href="http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</A> <A href="http://www.springframework.org/schema/aop" target=_blank>http://www.springframework.org/schema/aop</A> <A href="http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" target=_blank>http://www.springframework.org/schema/aop/spring-aop-2.0.xsd</A> <A href="http://www.springframework.org/schema/tx" target=_blank>http://www.springframework.org/schema/tx</A> <A href="http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" target=_blank>http://www.springframework.org/schema/tx/spring-tx-2.0.xsd</A>"> <bean id="TestJar" class="net.java2000.test.jar.TestJar"> <property name="message"> <value>Hello World</value> </property> </bean></beans>
2 發現了這個常見的異常
E:/test>java -cp . -jar MyProject.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/beans/factory/BeanFactory
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.BeanFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
3 經尋找,發現正確的MANIFEST.MF如下
Manifest-Version: 1.0
Main-Class: net.java2000.test.jar.TestJar
Class-Path: spring.jar
lib/commons-logging-1.1.jar
這裡特別說明一下
1)在 Class-Path: 後面有一個空格,切記
2)在 Class-Path: 後面寫上你的jar 用空格分開
3)如果需要換行,切記在上一行末尾一定要有一個空格,下一行的開頭一定要有一個空格
4)最後一行要是一個空行,否則Eclipse打包時有可能把你的Class-Path 給忽略掉
4 運行效果如下:
5 結論
1) 使用 java -cp 來設定 classpath 對於 jar來說是無效的,因為根據jar的安全規定,其內部的Class-Path 會起作用,外部的會被屏蔽掉(注意是屏蔽掉,不是覆蓋掉)
2) Java自身提供了一個設定classpath的方案,那就是使用命令列參數
-Xbootclasspath: 完全取代基本核心的Java class 搜尋路徑.
不常用,否則要重新寫所有Java 核心class
-Xbootclasspath/a: 尾碼在核心class搜尋路徑後面.常用!!
-Xbootclasspath/p: 首碼在核心class搜尋路徑前面.不常用,避免
引起不必要的衝突.
文法如下:
(分隔字元與classpath參數類似,unix使用:號,windows使用;)
java -Xbootclasspath/a:spring.jar;lib/commons-logging-1.1.jar -jar MyProject.jar
3)當然,你把jar放到 {Java_home}/jre/lib/ext 這個目錄下面也是可以的,應為JVM肯定會搜尋這個目錄。