1

So I am kind of at a dead end here. Have been troubleshooting for a half a day now. Using Hibernate JPA persistence in a Java application.

When running code from within IDE (IntelliJ 2018.1.5) it runs fine, however, when trying to run from jar via command line I get the following error (full stacktrace):

Exception in thread "main" org.hibernate.boot.archive.spi.ArchiveException: Could not build ClassFile
        at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.toClassFile(ClassFileArchiveEntryHandler.java:64)
        at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.handleEntry(ClassFileArchiveEntryHandler.java:47)
        at org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor.visitArchive(JarFileBasedArchiveDescriptor.java:147)
        at org.hibernate.boot.archive.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:47)
        at org.hibernate.boot.model.process.internal.ScanningCoordinator.coordinateScan(ScanningCoordinator.java:75)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:98)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:228)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:170)
        at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:181)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:129)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
        at com.gsr.metrics.repository.BaseRepository.<init>(BaseRepository.java:10)
        at com.gsr.metrics.repository.ProcessHistoryRepository.<init>(ProcessHistoryRepository.java:11)
        at com.gsr.metrics.FileProcessor.<init>(FileProcessor.java:24)
        at com.gsr.metrics.PostProcessor.run(PostProcessor.java:42)
        at com.gsr.metrics.PostProcessor.main(PostProcessor.java:28) Caused by: java.io.IOException: invalid constant type: 19 at 5
        at javassist.bytecode.ConstPool.readOne(ConstPool.java:1241)
        at javassist.bytecode.ConstPool.read(ConstPool.java:1172)
        at javassist.bytecode.ConstPool.<init>(ConstPool.java:185)
        at javassist.bytecode.ClassFile.read(ClassFile.java:807)
        at javassist.bytecode.ClassFile.<init>(ClassFile.java:148)
        at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.toClassFile(ClassFileArchiveEntryHandler.java:61)    
 ... 19 more

Build configuration is Maven and this is the Hibernate dependency entry

<?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>com.gsr.metrics</groupId>
    <artifactId>PostProcessor</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.gsr.metrics.PostProcessor</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.12.Final</version>
        </dependency>

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

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.4</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.0</version>
        </dependency>

        <dependency>
            <groupId>com.beust</groupId>
            <artifactId>jcommander</artifactId>
            <version>1.72</version>
        </dependency>
    </dependencies>

</project>

I've tried different Hibernate versions but in all cases have gotten the same error.

Problem occurs when this statement is executed

em = Persistence.createEntityManagerFactory(persistenceUnitName).createEntityManager();
steve dunning
  • 153
  • 2
  • 11
  • That's your whole code ? – Rafał Sokalski Jun 18 '18 at 17:31
  • Possible duplicate of [Could not build ClassFile - ArchiveException](https://stackoverflow.com/questions/27202939/could-not-build-classfile-archiveexception) – Rafał Sokalski Jun 18 '18 at 17:32
  • Thanks, but already came across that post. There was no additional javassist lib that I could find and when I put an exclusion for javassist in the Hibernate dependency there is a class not found error which I think tells me that it is not brought in elsewhere. – steve dunning Jun 18 '18 at 17:41
  • Put your pom.xml – Rafał Sokalski Jun 18 '18 at 17:46
  • Try to update hibernate dependency to higher version e.g. 5.2.17.Final – Rafał Sokalski Jun 18 '18 at 17:58
  • Yes, I had already tried that as well. – steve dunning Jun 18 '18 at 18:37
  • I built a bare bones application with the same Hibernate dependency and it works. Trying to spot any differences between the 2 application's Jar files the only thing I notices is that the one that works has an embedded hibernate-jpa-2.1-api-1.0.0.Final.jar file in addition to an org/hibernate class file directory. The non-working one only has the class directory. Both also appear to have identical javassist directory. – steve dunning Jun 19 '18 at 18:31
  • Check what is your java -version in command line – Rafał Sokalski Jun 19 '18 at 18:36
  • java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode) – steve dunning Jun 19 '18 at 18:44

2 Answers2

2

I've stumbled upon the same problem and I found a similar solution. I was using log4j-core 2.11.1 and got the same error when running the application from the jar. I've followed your solution and changed the verison to log4j-core 2.8.2. After that I've still got an error, but this time it was a lack of dependeny: apparently it was missing one of the "com.fasterxml.jackson.core" package classes. So I've added the dependency (version 2.9.6) and now it's running.

Hope this helps some one because it was a very frustrating error.

1

After a great deal of trial and error I isolated the issue to some type of library conflict with log4j. Don't ask me what the exact problem was but changing the versions of one of the two made the problem go away. Did not have the time to perform any sort of detailed analysis on the underlying reason.

steve dunning
  • 153
  • 2
  • 11