Home > Back-end >  Pact Maven-Springboot workshop step 1 fails with IllegalStateException: Failed to get nested archive
Pact Maven-Springboot workshop step 1 fails with IllegalStateException: Failed to get nested archive

Time:10-18

I'm trying to follow the steps in Pact Maven-Springboot workshop but getting this error at step 1 when I run this command: java -jar target/product-catalogue-0.0.1-SNAPSHOT.jar:

C:\Users\57046\git\pact-workshop-Maven-Springboot-JUnit5\consumer>java -jar target/product-catalogue-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/logback-classic-1.2.3.jar
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:104)
        at org.springframework.boot.loader.archive.JarFileArchive$NestedArchiveIterator.adapt(JarFileArchive.java:237)
        at org.springframework.boot.loader.archive.JarFileArchive$NestedArchiveIterator.adapt(JarFileArchive.java:228)
        at org.springframework.boot.loader.archive.JarFileArchive$AbstractIterator.next(JarFileArchive.java:189)
        at org.springframework.boot.loader.ExecutableArchiveLauncher.createClassLoader(ExecutableArchiveLauncher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:55)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/logback-classic-1.2.3.jar'
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:288)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:274)
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:100)
        ... 6 more
Caused by: java.lang.IndexOutOfBoundsException
        at org.springframework.boot.loader.jar.AsciiBytes.<init>(AsciiBytes.java:73)
        at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.load(CentralDirectoryFileHeader.java:85)
        at org.springframework.boot.loader.jar.CentralDirectoryParser.parseEntries(CentralDirectoryParser.java:68)
        at org.springframework.boot.loader.jar.CentralDirectoryParser.parse(CentralDirectoryParser.java:57)
        at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:137)
        at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:123)
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:319)
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:296)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:285)
        ... 8 more

As suggested by Stephen, I checked if the mentioned jar was corrupt. mvn dependency:analyze printed this:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  29.455 s
[INFO] Finished at: 2022-10-11T15:26:10 03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.1.2:analyze (default-cli) on project product-catalogue: Cannot analyze dependencies: invalid CEN header (bad signature) -> [Help 1]

I deleted the jar file, run mvnw verify (with no errors and logging that the jar is downloaded again) and then mvn dependency:analyze again, which then gave me:

[WARNING] Used undeclared dependencies found:
[WARNING]    org.springframework.boot:spring-boot:jar:2.4.3:compile
[WARNING]    org.springframework:spring-web:jar:5.3.4:compile
[WARNING]    org.springframework:spring-beans:jar:5.3.4:compile
[WARNING]    org.springframework:spring-context:jar:5.3.4:compile
[WARNING]    org.springframework.boot:spring-boot-autoconfigure:jar:2.4.3:compile
[WARNING] Unused declared dependencies found:
[WARNING]    org.springframework.boot:spring-boot-starter-web:jar:2.4.3:compile
[WARNING]    org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.4.3:compile
[WARNING]    org.springframework.boot:spring-boot-starter-test:jar:2.4.3:test
[WARNING]    org.projectlombok:lombok:jar:1.18.16:provided
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.768 s
[INFO] Finished at: 2022-10-11T16:00:05 03:00

But I'm still getting the same exception when I try to run the code.

As per another suggestion I run mvn spring-boot:run but got:

C:\Users\57046\git\pact-workshop-Maven-Springboot-JUnit5\consumer>mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< io.pact.workshop:product-catalogue >-----------------
[INFO] Building product-catalogue 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.4.3:run (default-cli) > test-compile @ product-catalogue >>>
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ product-catalogue ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 5 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ product-catalogue ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ product-catalogue ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\57046\git\pact-workshop-Maven-Springboot-JUnit5\consumer\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ product-catalogue ---
[INFO] No sources to compile
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.4.3:run (default-cli) < test-compile @ product-catalogue <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.3:run (default-cli) @ product-catalogue ---
[INFO] Attaching agents: []
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:39)
        at org.apache.commons.logging.LogAdapter$Log4jLog.<init>(LogAdapter.java:165)
        at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
        at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
        at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:203)
        at io.pact.workshop.product_catalogue.Application.main(Application.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 8 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.030 s
[INFO] Finished at: 2022-10-11T16:18:48 03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.4.3:run (default-cli) on project product-catalogue: Application finished with exit code: 1 -> [Help 1]

which is further confusing as it gets NoClassDefFoundError for another class.

CodePudding user response:

I think there is a missing entry in the consumer\pom.xml. When I added this dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId> 
</dependency>

as per this answer, it started to work.

  • Related