原文地址:http://www.java2000.net/viewthread.jsp?tid=6053
转载请注明上述链接或者CSDN的链接
1 今天彻底测试了jar程序
TestJar.java
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 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 运行效果如下:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/java2000_net/2008-06-08_071249.png)
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肯定会搜索这个目录。
<script type="text/javascript">
</script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>
分享到:
相关推荐
1. **Spring框架**:Spring是一个开源的应用框架,它提供了全面的企业级应用开发解决方案,包括依赖注入(DI)、面向切面编程(AOP)、数据访问/集成、Web框架、事务管理、远程调用、测试工具等。Spring以其模块化和...
这是Oracle提供的一种传统解决方案,用于通过ODBC桥接Java和Oracle。虽然它在某些情况下仍可用,但通常不推荐,因为它性能较低,且需要在系统中安装ODBC驱动。 4. Universal Connection Pool (UCP): UCP是Oracle...
使用IDEA启动项目遇见ClassNotFoundException的解决方案 在使用IDEA启动项目时,可能会遇见ClassNotFoundException的错误,导致项目无法启动。本文将详细介绍使用IDEA启动项目遇见ClassNotFoundException的正确解决...
SQLJDBC.jar是微软提供的Java数据库连接器,用于在Java应用程序中与SQL Server...如果你的项目要求与旧版本的SQL Server兼容,或者你只是需要一个简单而直接的解决方案,那么这个版本的驱动程序是一个不错的选择。
- **ClassNotFound Exception**:这可能是因为缺少了必要的依赖JAR,或者JAR版本不正确。 - **Struts Action Mapping找不到**:可能是因为struts-config.xml配置错误,Action或ActionMapping没有正确定义。 - **Null...
在部署 Java Web 项目到 Tomcat 服务器时,经常会遇到各种问题,例如 Class Not Found、jar 包冲突、环境变量配置不正确等等。这些问题如果不及时解决,将会导致项目无法正常运行,影响项目的开发和部署。 二、解决...
总的来说,SQL Server驱动是Java开发者与SQL Server数据库进行交互的重要桥梁,`msbase.jar`、`mssqlserver.jar`和`msutil.jar`这三者结合,提供了一个完整的、功能丰富的JDBC驱动解决方案,支持各种数据库操作和...
1. **ClassNotFound异常**:检查EJB的依赖库是否已正确导入,确保JAR包在服务器的类路径中。 2. **NamingException**:检查JNDI查找字符串是否正确,通常格式为"ejb/local:BeanName"或"ejb/remote:BeanName"。 3. ...
在集成过程中可能会遇到各种问题,例如:404错误、ClassNotFound异常等。这些问题通常与配置文件的错误、类路径问题、过滤器配置不当或JAR包缺失有关。当遇到这些问题时,检查web.xml和struts.xml的配置,确保所有...
8. **错误排查**:如果遇到“Class not found”异常,可能是因为JDBC驱动没有被正确地添加到项目的类路径中。如果出现连接失败,检查网络配置、数据库服务状态、以及连接字符串的准确性。 总的来说,Oracle数据库...
其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...