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 ...
随机推荐
- 带你剖析淘宝TDDL——Matrix层的分库分表配置与实现
前言 在开始讲解淘宝的TDDL(Taobao Distribute Data Layer)技术之前,请允许笔者先吐槽一番.首先要开喷的是淘宝的社区支持做的无比的烂,TaoCode开源社区上面,几乎从来 ...
- 浪潮openStack云
- 关于C++成员函数指针的使用
在做项目的时候,遇到了在类中根据不同的调用函数,在被调用函数的某处需要做不同的处理,本来就想着直接在类中设个标记变量判断下就好了,不过觉得这样代码可能看起来会有些凌乱,而且效率估计有些低,于是想起来使 ...
- Centos7 / RHEL 7 双网卡绑定
http://www.cnblogs.com/hukey/p/6224969.html 1. 简要 双网卡绑定技术在centos7中使用了teaming技术,而在rhel6/centos7中使用 ...
- Octave中调用hist出现broken pipe some output may be lost octave的解决(Mac)
参考:http://octave.1599824.n4.nabble.com/Mac-OS-X-Mountain-Lion-Octave-can-not-execute-sombrero-td4643 ...
- leetcode260
public class Solution { public int[] SingleNumber(int[] nums) { var dic = new Dictionary<int, int ...
- Myeclipse报错:“Versions of Spring facet could not be detected”的解决方法
解决方法如下: VERSION OF SPRING FACET COULD NOT BE DETECTED. The migration process needs to detect the cor ...
- 利用Console来学习、调试JavaScript
一 什么是 Console Console 是用于显示 JS和 DOM 对象信息的单独窗口.并且向 JS 中注入1个 console 对象,使用该对象 可以输出信息到 Console 窗口中. 二 ...
- CBCentralManager Class 的相关分析
Overview 总体概述 CBCentralManager objects are used to manage discovered or connected remote peripheral ...
- java基础之日期时间工具类
package zy.test; import java.sql.Timestamp; import java.text.ParseException; import java.text.ParseP ...