0

As part of our Consumer Driven Contract implementation, we are using PACT dependencies in our project to convert JSON to PACT file.

PACT dependencies added in Pom.xml file -

pact-jvm-consumer-junit_2.11 (version - 3.5.0)

pact-jvm-model (version - 3.5.0)

pact-jvm-pact-broker (version - 3.6.7)

These PACT dependencies are downloaded and bundled in jar using maven clean install command. And when we execute the jar on CloudOps VM and local server it works fine and gives the success response for the API. When the same jar is deployed successfully on PCF and we try to execute the API it returns NoClassDefFoundError for one of the PACT dependencies class - au.com.dius.pact.model.RequestResponsePact .

Below is the complete error from PCF logs -

2020, level: ERROR, thread: http-nio-8080-exec-8, loggerName: com.test.sample.common.aop.logging.LoggingAspect,

message: Exception in com.test.sample.cdcc.v1.process.CdccUploadProcessV1.cdcUploadedFiles() with cause = 'NULL' and exception = 'Could not initialize class au.com.dius.pact.model.RequestResponsePact',

error: java.lang.NoClassDefFoundError: Could not initialize class au.com.dius.pact.model.RequestResponsePact 2020-05-04T15:28:28.332+05:30 [APP/PROC/WEB/0] [OUT]

at au.com.dius.pact.consumer.dsl.PactDslResponse.toPact(PactDslResponse.java:265) 2020-05-04T15:28:28.332+05:30 [APP/PROC/WEB/0] [OUT]

at com.test.sample.cdcc.util.JsonToPactUtil.convertJSONtoPACT(JsonToPactUtil.java:47)

Below are few changes which we tried -

  1. Switching these dependencies to lower version

  2. Tried adding maven assembly plugin and creating a fat jar with all dependencies

  3. Modifying scope of the dependencies to provided and compile

  4. Adding class path in Maven jar plugin

But all these did not helped us to solve this issue. Could you point us in the right direction in solving this issue?

Below are the couple of ways i tried creating jar

Option1:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>${project.build.directory}/lib</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

Option2:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>    
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin> 
Community
  • 1
  • 1
Ankur
  • 1
  • 1
  • Look at your JAR, the one you are pushing to Cloud Foundry (`cf push -p `) and confirm that the actual class file exists in it. To do that you'll need to unzip the JAR, find the JAR which has your class inside that and extract that JAR to confirm it is there. If it's not there, you have a packaging problem. The JAR file that gets push to Cloud Foundry must contain *everything* needed to run your app. If it is there, explain more about how you are packaging your JAR file, or perhaps provide an example that reproduces the problem. – Daniel Mikusa May 20 '20 at 19:56
  • @DanielMikusa - I have checked the jar, and the class file is present in the dependency jar file present in the lib folder in BOOT-INF. I have tried packaging this using couple of plugin one with maven-jar-plugin and other using maven-dependency-plugin, but still no luck. I have updated the question with options i tried to create the jar files. I am wondering how same jar works locally when i run with java -jar command but gives exception when deployed in PCF environment. – Ankur May 21 '20 at 07:32
  • Two things are different. 1.) You're not pushing a JAR file, what actually gets pushed is the contents of the JAR file, so the start command won't be `java -jar blah.jar` because there is no JAR file. 2.) The Java buildpack is responsible for determining your start command, it understands how to run several different types of application but it's possible your app is doing something it doesn't understand. Any chance you could link me to a sample app that reproduces the problem? – Daniel Mikusa May 22 '20 at 16:33
  • @DanielMikusa - Sorry but this application is on restricted environment. But onto your second point , i am not doing anything different in my application. It is just in my application i am using one method from (au.com.dius.pact.consumer.dsl.PactDslResponse.toPact(PactDslResponse.java:265) present in another jar library which is bundled in my application jar in lib folder. Did you mean this is not being interpreted properly in java buildpack , but when it runs through java - jar it works as they both work differently ? – Ankur May 31 '20 at 05:28
  • Any chance you could make a demo app that reproduces the problem, but that you can share? Just needs to be a similar set up and structure so that you get similar errors. There could be an issue with the classpath being used, and it can't find your library. It's really hard to say without being able to reproduce and investigate further. – Daniel Mikusa Jun 01 '20 at 20:51
  • @DanielMikusa - I think you were right, looks like it had an issue with the way jar was packaged. Got it working! Thanks a lot for the help! – Ankur Jun 21 '20 at 09:16
  • @Ankur we had the same issue on PCF. We definitely packed our JAR well. Initially particular endpoint worked fine but after two hours of runtime, we started getting NoClassDefFound on one of the library classes. Some people say that PCF might do some periodical cleanup of temp folders where jar file unpacks the classes. We still face this issue from time to time. PCF restaging sometimes helps. – walv Jun 06 '22 at 01:51

0 Answers0