jar运行发生ClassNotFound的完整解决方案

jar运行发生ClassNotFound的完整解决方案原文地址 http www java2000 net viewthread jsp tid 6053 转载请注明上述链接或者 CSDN 的链接 1 今天彻底测试了 jar 程序 TestJar java package net java2000 test jar import javax swing JOptionPane import org springframew

原文地址: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

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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd”>


Hello World


2 发现了这个常见的异常 E:/test>java -cp . -jar MyProject.jar Exception in thread “main” java.lang.NoClassDefFoundError: org/springframework/beans/factory/BeanFactoryCaused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.BeanFactory at java.net.URLClassLoader

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肯定会搜索这个目录。

编程小号
上一篇 2025-02-06 15:06
下一篇 2025-01-29 17:06

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/114669.html