1
Good morning, everyone,
In one project we’re using Hibernate, Log4j2, Gson and a few other things that you can see in pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>br.org.minhaempresa</groupId>
<artifactId>meuprojeto</artifactId>
<name>meuprojeto</name>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>***</name>
</organization>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>meuprojeto</finalName>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.5</version>
<configuration>
<mainClass>br.org.minhaempresa.meuprojeto.MainApp</mainClass>
<!-- only required if signing the jar file -->
<keyStoreAlias>example-user</keyStoreAlias>
<keyStorePassword>example-password</keyStorePassword>
<allPermissions>true</allPermissions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<useReleaseProfile>false</useReleaseProfile>
<checkModificationExcludes>
<checkModificationExclude>pom.xml</checkModificationExclude>
</checkModificationExcludes>
<tagNameFormat>version-@{project.version}</tagNameFormat>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>br.org.minhaempresa.meuprojeto.MainApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.build.finalName}-${project.version}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<!-- Impinj OctaneSDK -->
<dependency>
<groupId>com.impinj</groupId>
<artifactId>octanesdk</artifactId>
<version>1.22.0.30</version>
</dependency>
<!-- Pi4j dependency -->
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.1</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.7.Final</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- UaiCriteria -->
<dependency>
<groupId>uaihebert.com</groupId>
<artifactId>uaiCriteria</artifactId>
<version>4.0.0</version>
</dependency>
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<!-- PDFBox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.11</version>
</dependency>
<!-- Apache Commons lang -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
When running the program through Run as Java Application, everything works properly by connecting to my database. Then I’ll jar with all the dependencies(myprogram-0.0.1-SNAPSHOT-jar-with-dependencies). When running the application with java -jar meuprogram-0.0.1-SNAPSHOT-jar-with-dependencies.jar the application does not connect in the database and returns me the following message:
Unable to build entity manager factory
I opened the jar to see if persistence.xml is inside. It is! Inside the project it is in the src/main/Resources/META-INF folder.
My persistence.xml. Properties are in the connection class:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="crp" transaction-type="RESOURCE_LOCAL">
<!-- provedor/implementacao do JPA -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
public class Conexao {
private static final Logger logger = LogManager.getLogger("MainApp");
private static EntityManagerFactory emf = null;
public Conexao() throws Exception{
Map<String, Object> props = new HashMap<String, Object>();
props.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
props.put("hibernate.show_sql", "false");
props.put("hibernate.format_sql", "false");
props.put("hibernate.hbm2ddl.auto", "update");
props.put("hibernate.c3p0.acquire_increment", 2);
props.put("hibernate.c3p0.min_size", 2);
props.put("hibernate.c3p0.max_size", 10);
props.put("hibernate.c3p0.max_statements", 50);
props.put("hibernate.c3p0.timeout", 0);
props.put("hibernate.c3p0.idle_test_period", 3000);
props.put("hibernate.c3p0.checkoutTimeout", 15000);
props.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
props.put("javax.persistence.jdbc.url", "jdbc:mysql://localhost/db_crp");
props.put("javax.persistence.jdbc.user", "user");
props.put("javax.persistence.jdbc.password", "pass");
emf = Persistence.createEntityManagerFactory("crp", props);
}
public EntityManager getEntityManager() throws Exception {
//responsavel pela persistencia
if (emf != null)
return emf.createEntityManager();
return null;
}
public void close() {
if (emf != null){
emf.close();
logger.info("Fechando conexão com BD...");
}
}
}
Does anyone have any idea what might be going on?
Thank you very much.
Enter the code of your persistence.xml.
– Giuliana Bezerra
@Giulianabezerra edited my question with this information.
– Tulio F. Polachini
If when you run as java Application everything normally works in the jar should also work. Probably something must have been missing in the jar assembly. Maybe some classpath is missing in MANIFEST.mf. Like this . jar is being assembled?
– Sérgio Mucciaccia
@Sérgiomucciaccia executo mvn package and the jar with the depenências is generated. Did not know about MANIFEST.mf. It should be created automatically by Maven or I need to create it?
– Tulio F. Polachini
It is created automatically by Maven, but it would be good to check if the dependencies are all neat within the .jar. Are you using which ide? No eclipse has a plugin for this (export > java > runnable jar file), maybe it is easier to do directly by mvn.
– Sérgio Mucciaccia
That’s weird! You’re using the Maven package correctly, including I always use it this way and it works. I suggest you take this jar with dependencies generated and see if in its content all dependencies have actually been packaged. Unfortunately I don’t see a simpler way to debug it =/
– Giuliana Bezerra