前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jar运行发生ClassNotFound的完整解决方案

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

作者头像
全栈程序员站长
发布2022-09-13 10:14:42
3040
发布2022-09-13 10:14:42
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

原文地址: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/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 运行效果如下:

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

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 type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”> </script>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160337.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档