真正解决方案:java.lang.ClassNotFoundException: javax.xml.bind.JAXBException

真正解决方案:java.lang.ClassNotFoundException: javax.xml.bind.JAXBException

1. 故障描述

今天在使用JDK 9.0 环境下使用Hibernate 时候出现了这个错误,错误日志如下:

这里写图片描述

2. 故障原因分析

JAXB API是java EE 的API,因此在java SE 9.0 中不再包含这个 Jar 包。
java 9 中引入了模块的概念,默认情况下,Java SE中将不再包含java EE 的Jar包
而在 java 6/7 / 8 时关于这个API 都是捆绑在一起的

3.解决方案

3.1 解决方案一

降低JDK 9 版本到 JDK 6/7/8

3.2 解决方案二(亲测可行)

手动加入这些依赖Jar包

要解决这个问题,我导入了下面这四个Jar包修复成功。

javax.activation-1.2.0.jar

jaxb-api-2.3.0.jar

jaxb-core-2.3.0.jar

jaxb-impl-2.3.0.jar

下载上面这些文件和复制他们到libs文件夹下,
添加他们导入到Build Path中
重新运行即可

3.3 解决方案三

Maven项目可添加如下依赖:

<!-- Java 6 = JAX-B Version 2.0   -->
<!-- Java 7 = JAX-B Version 2.2.3 -->
<!-- Java 8 = JAX-B Version 2.2.8 -->
<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

Tips:
建议使用中心仓库,否则可能某些jar找不到:

HTTP: http://repo1.maven.org/maven2
HTTPS:https://repo1.maven.org/maven2

原文地址

测试代码:

public class DatabaseConnectionTools {
    private static final SessionFactory ourSessionFactory;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        final Session session = getSession();
        try {
            System.out.println("querying all the managed entities...");
            final Metamodel metamodel = session.getSessionFactory().getMetamodel();
            for (EntityType<?> entityType : metamodel.getEntities()) {
                final String entityName = entityType.getName();
                final Query query = session.createQuery("from " + entityName);
                System.out.println("executing: " + query.getQueryString());
                for (Object o : query.list()) {
                    System.out.println("  " + o);
                }
            }
        } finally {
            session.close();
        }
    }
}

Tips: 如果做完上述操作,仍然报错,请检查out 文件夹下的lib 文件夹中是否包含刚添加的几个Jar包,如果没有,那么请继续下面的操作:
这里写图片描述
选中project,然后右键选择open module settings
这里写图片描述
然后检查Problem选项卡,检查右侧是否有‘Fixed’ 字样,如果有,请点击’Fixed’
这里写图片描述

技术宅星云 CSDN认证博客专家 Java MySQL Redis
技术宅星云(网名),英文名fairy,先后曾在惠普,北京中国航信工作, 目前担任北京蛙跳科技有限公司后端高级开发工程师,负责公司短视频App应用后台,擅长JAVA后端技术,CSDN博客专家。
巨量引擎SDK接入报错java.lang.ClassNotFoundException: com.android.id.impl.IdProviderImpl 求救,非常感谢! ``` 05/15 16:49:59: Launching 'app' on Pixel 2 API 28. $ adb shell am start -n "com.cn.leaf.demo/com.cn.leaf.demo.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Connected to process 6411 on device 'Pixel_2_API_28 [emulator-5554]'. Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page. I/MultiDex: VM with version 2.1.0 has multidex support Installing application VM has multidex support, MultiDex support library is disabled. W/om.cn.leaf.dem: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking) Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking) Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking) Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking) Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking) W/om.cn.leaf.dem: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection) W/om.cn.leaf.dem: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection) W/om.cn.leaf.dem: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking) D/TrackerDr: f#init: D/TrackerDr: i#init: E/i#IdentifierManager: reflect exception! java.lang.ClassNotFoundException: com.android.id.impl.IdProviderImpl at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:453) at java.lang.Class.forName(Class.java:378) at com.bytedance.a.i$a.<clinit>(SourceFile:195) at com.bytedance.a.i$a.a(SourceFile:185) at com.bytedance.a.i.a(SourceFile:106) at com.bytedance.a.i.<init>(SourceFile:49) at com.bytedance.a.i.b(SourceFile:41) at com.bytedance.a.i.a(SourceFile:34) at com.bytedance.a.g.a(SourceFile:46) at com.bytedance.a.b.a(SourceFile:33) at com.bytedance.applog.b.i.<init>(SourceFile:67) at com.bytedance.applog.AppLog.init(SourceFile:106) at com.cn.leaf.demo.MainActivity.initConfig(MainActivity.java:133) at com.cn.leaf.demo.MainActivity.PerMissions(MainActivity.java:186) at com.cn.leaf.demo.MainActivity.judgeBrand(MainActivity.java:265) at com.cn.leaf.demo.MainActivity.onCreate(MainActivity.java:56) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.id.impl.IdProviderImpl" on path: DexPathList[[zip file "/data/app/com.cn.leaf.demo-XfKMKxO94jFGo_lzV_gX6A==/base.apk"],nativeLibraryDirectories=[/data/app/com.cn.leaf.demo-XfKMKxO94jFGo_lzV_gX6A==/lib/x86, /data/app/com.cn.leaf.demo-XfKMKxO94jFGo_lzV_gX6A==/base.apk!/lib/x86, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134) at java.lang.ClassLoader.loadClass(ClassLoader.java:379) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at java.lang.Class.classForName(Native Method)  at java.lang.Class.forName(Class.java:453)  at java.lang.Class.forName(Class.java:378)  at com.bytedance.a.i$a.<clinit>(SourceFile:195)  at com.bytedance.a.i$a.a(SourceFile:185)  at com.bytedance.a.i.a(SourceFile:106)  at com.bytedance.a.i.<init>(SourceFile:49)  at com.bytedance.a.i.b(SourceFile:41)  at com.bytedance.a.i.a(SourceFile:34)  at com.bytedance.a.g.a(SourceFile:46)  at com.bytedance.a.b.a(SourceFile:33)  at com.bytedance.applog.b.i.<init>(SourceFile:67)  at com.bytedance.applog.AppLog.init(SourceFile:106)  at com.cn.leaf.demo.MainActivity.initConfig(MainActivity.java:133)  at com.cn.leaf.demo.MainActivity.PerMissions(MainActivity.java:186)  at com.cn.leaf.demo.MainActivity.judgeBrand(MainActivity.java:265)  at com.cn.leaf.demo.MainActivity.onCreate(MainActivity.java:56)  at android.app.Activity.performCreate(Activity.java:7136)  at android.app.Activity.performCreate(Activity.java:7127)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6669)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  ```
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页
实付 59.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值