Original address: http://www.java2000.net/viewthread.jsp? Tid = 6053
Blog: http://blog.csdn.net/java2000_net/archive/2008/06/08/2522408.aspx
1. I thoroughly tested the jar program today.
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 = new classpathxmlapplicationcontext (New String [] {"applicationcontext. XML "}); testjar OBJ = (testjar) beanfactory. getbean ("testjar"); system. out. println (obj. getmessage (); joptionpane. showmessagedialog (null, "jar content", "jar title", 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. Found this common exception
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. After searching, the correct manifest. mf is found as follows:
Manifest-version: 1.0
Main-class: net. java2000.test. Jar. testjar
Class-path: Spring. Jar
LIB/commons-logging-1.1.jar
Here is a special description
1) there is a space behind class-path. Remember
2) In Class-path: Your jar files are separated by spaces.
3) if you need to wrap a line, remember that there must be a space at the end of the previous line and a space at the beginning of the next line.
4) if the last line is a blank line, otherwise, your class-path may be ignored during eclipse packaging.
4. The running effect is as follows:
5 conclusion
1) using Java-CP to set classpath is invalid for jar, because according to the security regulations of jar, its internal class-path will take effect, the outside will be blocked (note that it is blocked, not overwritten)
2) Java itself provides a classpath setting solution, that is, using command line parameters
-Xbootclasspath: Completely replaces the Java class search path with the basic core.
Not commonly used. Otherwise, you must re-write all Java core classes.
-Xbootclasspath/A: The suffix is behind the core Class search path. It is commonly used !!
-Xbootclasspath/P: prefix before core Class search path. not commonly used. Avoid
Cause unnecessary conflicts.
Syntax:
(The Delimiter is similar to the classpath parameter. UNIX uses the "No." And Windows uses it ;)
Java-xbootclasspath/A: Spring. jar; LIB/commons-logging-1.1.jar-jar myproject. Jar
3) Of course, you can put the JAR file under the {java_home}/JRE/lib/EXT directory. The JVM will certainly search for this directory.