Hibernate配置文件未找到。

huangapple go评论77阅读模式
英文:

Hibernate config file not found

问题

I am working on a java plugin that utilizes Hibernate JPA for my database interactions. I am using a Hibernate configuration file to accomplish this task.

When I export my project into a jar file and run it, I get the following error:

[23:23:12] [Server thread/ERROR]: Error occurred while enabling nationsatwar v1.1 (Is it up to date?)
java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
        at net.mcnations.nationsatwar.PluginMain_Load.onEnable(PluginMain_Load.java:31) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        ...
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
        ...

With the actual error being thrown on this line of code:

SessionFactory factory = new Configuration().configure("hibernate.cfg.xml")

This immediately suggests my hibernate.cfg.xml file is not being exported in the project. I checked the project files in the export wizard, and I could not even find the configuration file in the window.

(Note the missing hibernate.cfg.xml file!!! (should be under the worldedit package))

Finally, in case it's relevant, I am using the following dependency in my POM for Hibernate:

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.18.Final</version>
</dependency>

What am I doing wrong?

英文:

I am working on a java plugin that utilizes Hibernate JPA for my database interactions. I am using a Hibernate configuration file to accomplish this task.

When I export my project into a jar file and run it, I get the following error:

    [23:23:12] [Server thread/ERROR]: Error occurred while enabling nationsatwar v1.1 (Is it up to date?)
java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
        at net.mcnations.nationsatwar.PluginMain_Load.onEnable(PluginMain_Load.java:31) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugin(CraftServer.java:493) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugins(CraftServer.java:407) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at net.minecraft.server.v1_16_R1.MinecraftServer.loadWorld(MinecraftServer.java:438) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at net.minecraft.server.v1_16_R1.DedicatedServer.init(DedicatedServer.java:219) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:810) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at java.lang.Thread.run(Thread.java:830) [?:?]
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
        at java.net.URLClassLoader.findClass(URLClassLoader.java:436) ~[?:?]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:167) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:96) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:588) ~[?:?]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[?:?]

With the actual error being thrown on this line of code:

SessionFactory factory = new Configuration().configure(&quot;hibernate.cfg.xml&quot;)

This immediately suggests my hibernate.cfg.xml file is not being exported in the project. I checked the project files in the export wizard, and I could not even find the configuration file in the window.

Hibernate配置文件未找到。

(Note the missing hibernate.cfg.xml file!!! (should be under the worldedit package))

Finally, in case its relevant, I am using the following dependency in my POM for Hibernate:

&lt;!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
	&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
	&lt;version&gt;5.4.18.Final&lt;/version&gt;
&lt;/dependency&gt;

What am I doing wrong?

答案1

得分: 1

如Alex Funk在评论中所述:

> 尝试执行新的 Configuration 时,Java 找不到 org/hibernate/cfg/Configuration

问题在于依赖项未打包到 JAR 文件中。因此,如果您未在类路径中指定 .jar 并在运行时提供它,JVM 将无法创建或访问依赖项的类实例。在这种情况下,错误指示找不到类 org.hibernate.cfg.Configuration。因此,您有两种方法:

方法1. 创建带有依赖项的 JAR 文件(更大的 JAR 文件,但建议使用)

要执行此操作,您必须覆盖 Maven 组装插件并进行以下配置:

&lt;plugins&gt;
    &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.3.0&lt;/version&gt;
        &lt;executions&gt;
            &lt;execution&gt;
                &lt;phase&gt;package&lt;/phase&gt;
                &lt;goals&gt;
                    &lt;goal&gt;single&lt;/goal&gt;
                &lt;/goals&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;jar-with-dependencies&lt;/descriptorRefs&gt;
                    &lt;appendAssemblyId&gt;false&lt;/appendAssemblyId&gt;
                &lt;/configuration&gt;
            &lt;/execution&gt;
        &lt;/executions&gt;
    &lt;/plugin&gt;
&lt;/plugins&gt;

然后,您可以通过以下方式生成 JAR 文件:

mvn install

方法2. 将 Hibernate 添加到 .jar 类路径并在运行时提供

我不建议使用此方法,因为它破坏了插件的主要优势之一。用户将不得不下载库并将其放在服务器的 lib 文件夹中。因此,插件将需要更多的安装步骤,而不仅仅是将插件 "插入" 服务器插件文件夹中。

Maven 配置将如下所示:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;archive&gt;
            &lt;manifest&gt;
                &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                &lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
                &lt;mainClass&gt;com.example.MainClass&lt;/mainClass&gt;
            &lt;/manifest&gt;
        &lt;/archive&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

然后,在服务器中,用户将不得不将库 JAR 文件提供到插件文件夹内的 lib 文件夹中:

MyServer
|--- plugins
|    |--- lib
|          |--- hibernate-core-5.4.18.Final.jar
|    |--- YourPlugin-1.0.0.jar
|--- spigot-1.16.1.jar
... 其他服务器文件和文件夹

然后执行:

mvn install

请告诉我这是否解决了您的问题!

英文:

As stated Alex Funk in the comments:

> java do not find org/hibernate/cfg/Configuration when trying to execute new Configuration

The problem is that the dependency is not packed into the jar file. Then, if you don't specify the .jar in the classpath and provide it at runtime, the JVM will not be able to create or access instances of classes of the dependency. In this case, the error indicates that the class org.hibernate.cfg.Configuration is not found. So you have two approaches:

Approach 1. Create a jar with the dependencies (Bigger .jar but recommended)

To do this you must override the maven assembly plugin and configure it the following way:

&lt;plugins&gt;
    &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.3.0&lt;/version&gt;
        &lt;executions&gt;
            &lt;execution&gt;
                &lt;phase&gt;package&lt;/phase&gt;
                &lt;goals&gt;
                    &lt;goal&gt;single&lt;/goal&gt;
                &lt;/goals&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;jar-with-dependencies&lt;/descriptorRefs&gt;
                    &lt;appendAssemblyId&gt;false&lt;/appendAssemblyId&gt;
                &lt;/configuration&gt;
            &lt;/execution&gt;
        &lt;/executions&gt;
    &lt;/plugin&gt;
&lt;/plugins&gt;

Then you can generate the jar via:

mvn install

Approach 2. Add the hibernate into the .jar classpath and provide it on runtime

I do not recommend this approach because it breaks one of the main advantages of plugins. The user will have to download the library and place it in a lib folder in its server. So the plug-in will require more installation steps by the end-user instead or just 'plugging in' the plugin in the server plugins folder.

The maven configuration will be:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;archive&gt;
            &lt;manifest&gt;
                &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                &lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
                &lt;mainClass&gt;com.example.MainClass&lt;/mainClass&gt;
            &lt;/manifest&gt;
        &lt;/archive&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

Then in the server, users will have to provide the library jar into a lib folder inside the plugins folder:

MyServer
|--- plugins
|    |--- lib
|          |--- hibernate-core-5.4.18.Final.jar
|    |--- YourPlugin-1.0.0.jar
|--- spigot-1.16.1.jar
... other server files and folders

Then execute:

mvn install

Let me know if this solves your problem!

huangapple
  • 本文由 发表于 2020年7月29日 12:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63146447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定