java.lang.NoClassDefFoundError: Could not initialize class com解决方案
编写的时候遇到这样一个bug, java.lang.NoClassDefFoundError: Could not initialize class com

纠结了两天多,但是,没有找到答案,这个问题的多方提示就是
(1)缺JAR包 (2)JAR包冲突 (3)TOMCAT或Eclipse全路径中有空格
这些答案都没有解决我的问题,这个错误提示是说我的那个包没法启动,很多显示都是是什么静态定义必须trycatch,但是我的这个自己简历的公共类,这些都写了.
public class MybatisUtil {
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
private static SqlSessionFactory sqlSessionFactory;
/**
* 加载位于src/mybatis.xml配置文件
*/
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 禁止外界通过new方法创建
*/
private MybatisUtil() {
}
/**
* 获取SqlSession
*/
public static SqlSession getSqlSession() {
//从当前线程中获取SqlSession对象
SqlSession sqlSession = threadLocal.get();
//如果SqlSession对象为空
if (sqlSession == null) {
//在SqlSessionFactory非空的情况下,获取SqlSession对象
sqlSession = sqlSessionFactory.openSession();
//将SqlSession对象与当前线程绑定在一起
threadLocal.set(sqlSession);
}
//返回SqlSession对象
return sqlSession;
}
/**
* 关闭SqlSession与当前线程分开
*/
public static void closeSqlSession() {
//从当前线程中获取SqlSession对象
SqlSession sqlSession = threadLocal.get();
//如果SqlSession对象非空
if (sqlSession != null) {
//关闭SqlSession对象
sqlSession.close();
//分开当前线程与SqlSession对象的关系,目的是让GC尽早回收
threadLocal.remove();
}
}
}
然后,我进行了另一个测试,连接测试
public class TestConnection {
@Test
public void testConnection() throws Exception {
Connection connection = MybatisUtil.getSqlSession().getConnection();
System.out.println(connection!=null?"连接成功":"连接失败");
}
}
发现是配置文件的错误,修改配置文件,连接就成功了,其他的就正确了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <properties resource="jdbc.properties"/> <!-- 别名标签 -->
<typeAliases>
<typeAlias type="com.liuya.demo.mybatis.dysnamic.pojo.NewsLabel"
alias="NewsLabel"/>
</typeAliases> <!-- 配置运行的数据库环境 -->
<environments default="mysqlenvironment">
<environment id="mysqlenvironment">
<!-- 連接池在本地连接中使用,在SSM中不用,用C3P0和DBCP -->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <!-- 连接映射文件 -->
<mappers>
<!-- 最终使用的都是package -->
<mapper resource="com\liuya\demo\mybatis\dysnamic\mapper\NewsLabelMapper.xml"/>
</mappers>
</configuration>
所以要好好的理解Mybatis的框架和配置标签的使用,这是好使版本的配置
java.lang.NoClassDefFoundError: Could not initialize class com解决方案的更多相关文章
- Java 上传文件到 SFTP 抛异常 java.lang.NoClassDefFoundError: Could not initialize class sun.security.ec.SunEC 的解决办法
最近从 Op 那里报来一个问题,说是SFTP上传文件不成功.拿到的 Exception 如下: Caused by: java.lang.NoClassDefFoundError: Could not ...
- 异常:Caused by: java.lang.NoClassDefFoundError: Could not initialize class net.sf.log4jdbc.Properties
参考文章: 使用Log4jdbc-log4j2监听MyBatis中运行的SQL和Connection 使用 log4jdbc格式化输出SQL,maven配置如下: <dependency> ...
- java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.util.JSONUtils
页面报错: root: java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.util.JSONUtils 出错 ...
- 异常-----java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.core.KeyFactory
SSH 类库问题 java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer2009- ...
- hibernate 解决 java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.xxx 这类的问题
<!-- 解决 java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.intern ...
- java.lang.NoClassDefFoundError: Could not initialize class xxx 原因
一.问题及原因 程序里有个工具类,主要是调用它的静态方法来发送mq. 调用场景如下: 结果这两天报了个错: java.lang.NoClassDefFoundError: Could not init ...
- java.lang.NoClassDefFoundError: Could not initialize class org.jfree.chart.JFreeChart
最近在进行利用jfreechart生成图表时发现,项目在本地运行的好好的,一部署到服务器(linux系统)上就不行,报这样的错误: java.lang.NoClassDefFoundError: Co ...
- exception java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
exception java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironmen ...
- 日志异常:java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.impl.StaticLoggerBinder
今天启动开发的项目,碰到了一个日志上的bug:java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.impl.Sta ...
随机推荐
- 第七章 Rolling update
7.1 实践 apiVersion: apps/v1beta1 kind: Deployment metadata: name: httpd spec: replicas: 3 template: m ...
- 5月9日上课笔记-网页定位、网页动画【HTML5】
一.网页定位 position: static (默认值) relative 相对定位(相对原来的位置) right left botton top absolute 绝对定位 fixed: 固定定位 ...
- Futures
Futures is a framework for expressing asynchronous code in C++ using the Promise/Future pattern. Ove ...
- 判断和调整library cache,data dictionary cache,buffer cache性能
Oracle SGA是oracle的内存结构,存放着oracle通过oracle进程读写的内存数据.sga分为好多组件,比如shared pool,buffer cache,redo log buff ...
- HDU1869 六度分离
/* 六度分离 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- Promise题目
setTimeout(function () { console.log(1); }, 0) new Promise(function executor(resolve) { console.log( ...
- Tkinter Dimensions
Tkinter Dimensions: 各种长度,宽度,和其他部件的尺寸可以在许多不同的单位描述 各种长度,宽度,和其他部件的尺寸可以在许多不同的单位描述. 如果您设置了尺寸为整数,它被假定为 ...
- Python实践练习目录
缘由 做中学才是最好的方法,通过这些项目来加强自己的Python掌握程度. 原则 成体系地学,不搞"题海战术" 通所有不如精一物,精一物方可通所有 走心学,忌浮躁 项目列表 字符串 ...
- 关于在Arduino中调用DS1302模块
DS1302时钟模块中的电池是起掉电保存作用的,在实际运行中必须给他的GND和VCC供电,否则得到的是错误的时间. 也就是说,电池是保存日期的,而无法提供芯片正常运行所需的电力. 从芯片引脚上可以看出 ...
- WOW研究资料收集
1,模拟服务器:trinity core, sunwell core等 参考:逍遥魔兽 2,大芒果:通用网游框架,带了WOW的模拟模块 参考资料: 大芒果论坛http://www.mangoscn.c ...