在 Eclipse 中运行程序时,使用 Hibernate 的 Eclipse 不加载 mysql 驱动程序 jar

Posted

技术标签:

【中文标题】在 Eclipse 中运行程序时,使用 Hibernate 的 Eclipse 不加载 mysql 驱动程序 jar【英文标题】:Eclipse using Hibernate not loading mysql driver jar when running program in Eclipse 【发布时间】:2017-03-09 16:40:08 【问题描述】:

我在 Eclipse 中运行示例 Hibernate 程序时低于堆栈跟踪。我知道它无法在找到 JDBC 驱动程序的地方找到 mysql-connector-*.jar。但是,我在 Eclipse 的“Maven 依赖项”列表中找到了这个 jar。我还验证了我的 eclipse .classpath 文件包含

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

条目。有什么建议 ? 下面是堆栈跟踪:

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:267)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:231)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:240)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at ProgramConfig.main(ProgramConfig.java:69)

Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class ["com.mysql.jdbc.Driver"]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:229)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:160)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:116)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:100)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:240)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:257)
... 13 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : "com.mysql.jdbc.Driver"
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226)
    ... 25 moreenter code here

我正在添加每个 cmets 尝试的 maven 依赖行 我都试过了:

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.3</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.3</version>
</dependency>

这是 POM 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>HibernateDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.3.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.3</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <!-- any other plugins -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                </execution>
            </executions>
            <configuration>
            <mainClass>ProgramConfig</mainClass>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

【问题讨论】:

你需要把驱动和数据源类链接起来 请在问题中添加您的pom.xml。确保 Eclipse 将您的项目视为maven 项目。将所有需要的库/jar 添加到 POM。不要手动编辑.classpath 在你的 pom.xml 中你定义了 mysql direver @JimHawkins 我添加了 POM 条目。 @Daniyar,我也尝试了提供的范围。它不工作。我使用 mvn clean build package 进行编译。之后,我尝试通过右键单击在 Eclipse 中运行该程序 -> 运行 -> 作为应用程序。我知道我可以使用打包的 jar 文件运行程序。但我想在 Eclipse 控制台中运行它。有什么建议吗? 【参考方案1】:

我有同样的问题。在 pom.xml 中保留 mysql-connector-java 并删除该属性 com.mysql.jc.jdbc.Driver 来自 hibernate.cfg.xml 为我解决了这个问题。

【讨论】:

以上是关于在 Eclipse 中运行程序时,使用 Hibernate 的 Eclipse 不加载 mysql 驱动程序 jar的主要内容,如果未能解决你的问题,请参考以下文章

Eclipse 在运行调试器时破坏了带有空格字符的程序参数

为啥每次尝试运行 Python 程序时 Eclipse 都会要求我“构建”?

如何制作启动特定类的自定义Eclipse运行启动程序?

错误:缺少 JavaFX 运行时组件,需要在 Eclipse 中运行此应用程序

在 Intellij 中更新 Kotlin 后在 Eclipse 中运行应用程序时出错

Hadoop MapReduce 程序在 Eclipse 中运行良好,但在导出到 .jar 文件时运行良好