java - 使用可运行 jar 时未找到 persistence.xml

标签 java hibernate executable-jar persistence.xml uberjar

由于

,我在运行可运行的 jar 时遇到一些问题
HHH000318: Could not find any META-INF/persistence.xml file in the classpath

目录结构: enter image description here

JAR META-INF/ list 内容:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Antoniossss
Build-Jdk: 1.8.0_40
Main-Class: app.remote.Bootstrap
Class-Path: .

从根目录运行命令:

java -jar controller-1.0-jar-with-dependencies.jar

这导致了

13:19:50.634 [main] DEBUG o.h.b.r.c.i.ClassLoaderServiceImpl - Incoming config yielded no classloaders; adding standard SE ones
13:19:50.637 [main] INFO  o.h.j.b.i.PersistenceXmlParser - HHH000318: Could not find any META-INF/persistence.xml file in the classpath
13:19:50.638 [main] DEBUG o.h.jpa.HibernatePersistenceProvider - Located and parsed 0 persistence units; checking each
13:19:50.638 [main] DEBUG o.h.jpa.HibernatePersistenceProvider - Found no matching persistence units
13:19:50.639 [main] DEBUG o.h.b.r.c.i.ClassLoaderServiceImpl - Incoming config yielded no classloaders; adding standard SE ones
13:19:50.639 [main] INFO  o.h.j.b.i.PersistenceXmlParser - HHH000318: Could not find any META-INF/persistence.xml file in the classpath
13:19:50.639 [main] DEBUG o.h.jpa.HibernatePersistenceProvider - Located and parsed 0 persistence units; checking each
13:19:50.640 [main] DEBUG o.h.jpa.HibernatePersistenceProvider - Found no matching persistence units
13:19:50.640 [main] DEBUG o.h.b.r.c.i.ClassLoaderServiceImpl - Incoming config yielded no classloaders; adding standard SE ones
13:19:50.641 [main] INFO  o.h.j.b.i.PersistenceXmlParser - HHH000318: Could not find any META-INF/persistence.xml file in the classpath
13:19:50.641 [main] DEBUG o.h.jpa.HibernatePersistenceProvider - Located and parsed 0 persistence units; checking each
13:19:50.643 [main] DEBUG o.h.jpa.HibernatePersistenceProvider - Found no matching persistence units
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named registratorPU
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
        at 

META-INF/perstistence.xml is not found

但它位于 META-INF 目录中。而且config.cfg被正确读取,因此当前目录确实在类路径中。

对我来说奇怪的是以下命令运行得很好:

java -cp controller-1.0-jar-with-dependencies.jar; app.remote.Bootstrap

但是,如果我删除 jar 名称后面的分号 (;),则错误与 jar 午餐尝试期间的错误相同。

java -cp controller-1.0-jar-with-dependencies.jar app.remote.Bootstrap

你知道这里发生了什么吗? jar是用maven创建的

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>app.remote.Bootstrap</mainClass>
                        </manifest>
                        <manifestEntries>
                              <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

最佳答案

尝试在pom.xml中添加

<resources>
        <resource>
            <directory>path_to_your_resource_folder</directory>
            <filtering>true</filtering>
        </resource>
</resources>

path_to_your_resource_folderi 是项目文件夹路径中包含 persistence.xml 的目录的路径。 我建议您创建一些目录,例如 src/main/resources 并将 persistence.xml 粘贴到此处。然后path_to_your_resource_folder= src/main/resources

关于java - 使用可运行 jar 时未找到 persistence.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29648956/

相关文章:

Java 字符串模式识别

java - 为父实体 JPA-GAE 分配现有子实体

java - 在运行时动态禁用 hibernate 验证注释?

java - 为什么 xml 文件无法与可执行文件 jar 一起使用

java - 如何从java应用程序执行可运行的jar

java - 使用嵌入式 API 向节点添加标签

java - 如何在 Java 中读取 RabbitMQ 中的 header ?

java - spring 和 hibernate 的 web.xml 设置

java - Hibernate 别名 - 仍然执行 n+1 选择是否正常?

scala - 从 spark-submit 运行应用程序时从 jar 读取输入文件