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

jar运行发生ClassNotFound的完整解决方案原文地址:http://www.java2000.net/viewthread.jsp?tid=6053转载请注明上述链接或者CSDN的链接1今天彻底测试了jar程序TestJar.javapackagenet.java2000.test.jar;importjavax.swing.JOptionPane;importorg.springframework.beans.fa

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

原文地址: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 运行效果如下:
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.net/160337.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 树莓派 网络设置_树莓派4b教程

    树莓派 网络设置_树莓派4b教程概览你想做的第一件事一定是把你的树莓派连接到因特网上。在这节课里,你将会学到如何:使用网线连接到以太网在Raspbian和Occidentalis上使用无线网卡找到树莓派的IP地址使用有线网络最快的把树莓派接入到因特网的方法是使用一根以太网线把树莓派连接到你家的路由器上。当你把网线连入树莓派的时候,你就会看到网络LED灯开始闪烁了。对于大多数的家庭网络来说,你就不需要再做任何进一步的配置了。但为了…

    2022年9月13日
    0
  • phpstorm激活码2021.12.13【2021最新】

    (phpstorm激活码2021.12.13)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/ide…

    2022年3月30日
    45
  • 民谣歌词基本格式_歌词创作

    民谣歌词基本格式_歌词创作把LRC格式的歌词解析成一个JS对象,适用于大部分书写比较规范的LRC格式的歌词。

    2022年10月23日
    0
  • SQL语句中 left join 后用 on 还是 where,区别大了!

    SQL语句中 left join 后用 on 还是 where,区别大了!

    2022年2月19日
    43
  • Offline Explorer_手机chrome离线网页

    Offline Explorer_手机chrome离线网页提及离线浏览软件,我想大家都知道“元老级”的TeleportPro吧?一些网友可能还挺怀念的,毕竟它开了离线浏览之先河,为广大网友节省了不少银子,带来了不少便利,功不可没呵!然而时过境迁,“江山代有才人出”,一大批新的离线浏览软件登场了。这里我要向大家介绍的是一个设计全新的离线浏览软件——OfflineExplorer1.1。一、基本信息 OfflineExplorer1.1由MetaP…

    2022年9月8日
    0
  • 台式计算机连不上网,台式机插上网线连不上网怎么办?[通俗易懂]

    台式计算机连不上网,台式机插上网线连不上网怎么办?[通俗易懂]由于你提供的问题描述不够详细具体,可能性太多了;这里为你分析下常见的一些情况以供参考!1.要确定是不是只有这一个台式机不能上网,同一个路由器下的其他终端是可以上网的。如果是这样,那基本和路由器设置无关(特殊情况例外)。2.台式机插上网线后,本地连接是否正常link起来?如果本地连接为一个“×”,那说明没有link成功,检查台式机的网卡驱动是否安装正常。由于此时台式机上不了网,建议你通过其他可以…

    2022年6月26日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号