Skip to content

java.lang.NoClassDefFoundError: models/xxx error on 20.0 graphql-java library version #3086

Closed

Description

@SandQuattro

I use graphql-java with Play Framework 2.8.19 and Java 11 (was trying also with Java 8, same error)
in build.sbt i add dependency like this:
libraryDependencies ++= "com.graphql-java" % "graphql-java" % "20.0"

When application is started, and after i send my graphQL request, i've got an following error in console
If i set my dependency version to 18.3, everything is fine and i've got an result like:
{
"data": {
"productById": {
"id": "1",
"name": "ProductName",
"details": [
{
"id": "1",
"description": "The product detailed description.",
"display": "display",
"name": "product details"
}
]
}
}
}

Actually trying to start this example, but with upgraded library to 20.0, latest, version: https://github.com/manojgodara/GraphQLPlayFramework

ERROR message on version 20.0:
ERROR notprivacysafe.graphql.GraphQL Execution '4e7d7de4-89ce-4518-a389-5a3ab105101c' threw exception when executing : query : 'query test($id: String!) {
productById(id: $id) {
id
name
details {
id
description
display
name
}
}
}'. variables '{id=1}'

java.util.concurrent.CompletionException: java.lang.NoClassDefFoundError: models/Product
	at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
	at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
	at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:783)
	at java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:792)
	at java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2153)
	at graphql.execution.Execution.executeOperation(Execution.java:171)
	at graphql.execution.Execution.execute(Execution.java:102)
	at graphql.GraphQL.execute(GraphQL.java:557)
	at graphql.GraphQL.lambda$parseValidateAndExecute$11(GraphQL.java:476)
	at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995)
	at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137)
	at graphql.GraphQL.parseValidateAndExecute(GraphQL.java:471)
	at graphql.GraphQL.executeAsync(GraphQL.java:439)
	at controllers.GraphQLController.query(GraphQLController.java:118)
	at controllers.GraphQLController.postHandler(GraphQLController.java:86)
	at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$3(Routes.scala:73)
	at play.core.routing.HandlerInvokerFactory$$anon$9.resultCall(HandlerInvoker.scala:154)
	at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$3$$anon$4$$anon$5.invocation(HandlerInvoker.scala:115)
	at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:119)
	at play.http.DefaultActionCreator$1.call(DefaultActionCreator.java:33)
	at play.core.j.JavaAction.$anonfun$apply$8(JavaAction.scala:175)
	at scala.concurrent.Future$.$anonfun$apply$1(Future.scala:659)
	at scala.util.Success.$anonfun$map$1(Try.scala:255)
	at scala.util.Success.map(Try.scala:213)
	at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
	at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
	at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33)
	at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
	at play.core.j.HttpExecutionContext.$anonfun$execute$1(HttpExecutionContext.scala:64)
	at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:70)
	at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:59)
	at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:72)
	at scala.concurrent.impl.Promise$KeptPromise$Kept.onComplete(Promise.scala:372)
	at scala.concurrent.impl.Promise$KeptPromise$Kept.onComplete$(Promise.scala:371)
	at scala.concurrent.impl.Promise$KeptPromise$Successful.onComplete(Promise.scala:379)
	at scala.concurrent.impl.Promise.transform(Promise.scala:33)
	at scala.concurrent.impl.Promise.transform$(Promise.scala:31)
	at scala.concurrent.impl.Promise$KeptPromise$Successful.transform(Promise.scala:379)
	at scala.concurrent.Future.map(Future.scala:292)
	at scala.concurrent.Future.map$(Future.scala:292)
	at scala.concurrent.impl.Promise$KeptPromise$Successful.map(Promise.scala:379)
	at scala.concurrent.Future$.apply(Future.scala:659)
	at play.core.j.JavaAction.apply(JavaAction.scala:176)
	at play.api.mvc.Action.$anonfun$apply$4(Action.scala:82)
	at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:307)
	at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:41)
	at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
	at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:63)
	at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:100)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:85)
	at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:100)
	at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:49)
	at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(ForkJoinExecutorConfigurator.scala:48)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1067)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1703)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
Caused by: java.lang.NoClassDefFoundError: models/Product
	at graphql.schema.PropertyFetchingImpl.getPropertyValue(PropertyFetchingImpl.java:114)
	at graphql.schema.PropertyDataFetcherHelper.getPropertyValue(PropertyDataFetcherHelper.java:22)
	at graphql.schema.PropertyDataFetcher.getImpl(PropertyDataFetcher.java:131)
	at graphql.schema.PropertyDataFetcher.get(PropertyDataFetcher.java:112)
	at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:307)
	at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:286)
	at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:212)
	at graphql.execution.AsyncExecutionStrategy.execute(AsyncExecutionStrategy.java:55)
	at graphql.execution.ExecutionStrategy.completeValueForObject(ExecutionStrategy.java:680)
	at graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:472)
	at graphql.execution.ExecutionStrategy.completeField(ExecutionStrategy.java:422)
	at graphql.execution.ExecutionStrategy.lambda$resolveFieldWithInfo$1(ExecutionStrategy.java:214)
	at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:616)
	at java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:628)
	at java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:1996)
	at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:213)
	at graphql.execution.AsyncExecutionStrategy.execute(AsyncExecutionStrategy.java:55)
	at graphql.execution.Execution.executeOperation(Execution.java:156)
	... 52 common frames omitted
Caused by: java.lang.ClassNotFoundException: models.Product
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 70 common frames omitted

Activity

  1. bbakerman commented on Feb 4, 2023

    @bbakerman
    Member

    java.lang.NoClassDefFoundError means that something went wrong during the "class definition" phase of loading the class.

    Typically this is because some "static" initializer threw an exception and hence the class definition is invalid and the JVM will mark the class as so and throw java.lang.NoClassDefFoundError from then on.

    I this root cause here is the java.lang.ClassNotFoundException: models.Product - it cant find that class

    The graphql standard graphql "property fetcher" is using a new mechanism to find getter methods

    See

    https://github.com/graphql-java/graphql-java/releases/tag/v20.0

    The PropertyDataFetcher class is the most common data fetcher used in graphql-java. It uses Java reflection to get field values from objects based on field name.

    This was logically the following

    Method method = findMethod(fieldname);
    method.invoke(object);
    

    with the method lookup cached for performance reasons.

    However there is mechanism in the JVM that provides even faster object reflective access.

    See

    https://wttech.blog/blog/2020/method-handles-and-lambda-metafactory/
    https://www.optaplanner.org/blog/2018/01/09/JavaReflectionButMuchFaster.html

    java.lang.invoke.LambdaMetafactory#metafactory is an arcane mechanism that can be used to create virtual method lambdas that give fast access to call object methods. It turns out to be significantly faster that Java reflection and only marginally slower that directly invoking a method.

    Somehow this is failing on your system - perhaps because of class loader challenges between where the graphql-java classes are and where the models.Product classes are?

    Are you using funky Java modules or OSGI perchance?

    You can disable this new LambdaMetafactory mechanism JVM wide. Call graphql.schema.PropertyDataFetcherHelper#setUseLambdaFactory with a false value and this strategy wont be used.

    This might help you debug what's going on and we would love to know more about why this is not working.

    I suspect "funky class loaders" between where graphql-java classes are and your model classes are but its hard to know from this distance

  2. SandQuattro commented on Feb 5, 2023

    @SandQuattro
    Author

    by the way, tried latest version on maven repository: 230521-nf-execution: "com.graphql-java" % "graphql-java" % "230521-nf-execution",
    no errors at all, works fine, got the result, problem only on 20.0

  3. dondonz commented on Feb 5, 2023

    @dondonz
    Member

    The version 230521-nf-execution is a rather old one from 2021, I tried to delete it but Maven doesn't make that easy sadly.

    Our main releases are listed on GitHub, and our latest build releases start with 0.0.0

  4. coolersham commented on Feb 13, 2023

    @coolersham

    Facing the exact same problem in a Quarkus Kotlin environment. Disabling the new LambdaMetafactory approach as mentioned by @bbakerman resolves the problem. However, this is not really a satisfactory solution because the performance benefit is lost.

    @SandQuattro did you come across a solution?

  5. SandQuattro commented on Feb 13, 2023

    @SandQuattro
    Author

    Facing the exact same problem in a Quarkus Kotlin environment. Disabling the new LambdaMetafactory approach as mentioned by @bbakerman resolves the problem. However, this is not really a satisfactory solution because the performance benefit is lost.

    @SandQuattro did you come across a solution?

    not yet. Downgraded to 18.3 version and it's fine for me

  6. bbakerman commented on Feb 18, 2023

    @bbakerman
    Member

    Can the people affected by this issue please outline more debugging information.

    It would appear that java.lang.invoke.LambdaMetafactory#metafactory performance trick is not working for some class loader set ups.

    Its is certainly not a common mechanism so its more than possible that specific "class loader" setups such as OSGI or ones with fancy security managers could pose a problem.

    As mentioned above this mechanism can be turned off via graphql.schema.PropertyDataFetcherHelper#setUseLambdaFactory which statically turns it off for the JVM (static boolean).

    Disabling the new LambdaMetafactory approach as mentioned by @bbakerman resolves the problem. However, this is not really a satisfactory solution because the performance benefit is lost.

    I really need more details on class loaders and JVMs used. I suspect strongly its some more "complex" JVM setups in play here.

    For example in the Quarkus case - is Graal Native being used?

  7. bbakerman commented on Feb 19, 2023

    @bbakerman
    Member

    This is what I think is happening

    There is a SomeTarget.class out there had has a methods we are going to invoke for a property fetch. And we have a someTarget object of that class to invoke.

    The java.lang.invoke.LambdaMetafactory#metafactory creates a mechanism that looks like this

    Function<Object,Object> fetchFN   ->  someTarget.methodToInvoke()
    

    The property fetcher code then invokes that function that is really a way to invoke some proxy code on the someTarget.

    Object value = fetchFN.apply(someTarget);
    

    However for some people above it is blowing up with LinkageErrors. This part I dont know know why.

    And to complicate it - the code is able to use Reflection onto someTarget and have that succeed.

    The class is not found in the Lambda case

    Caused by: java.lang.NoClassDefFoundError: models/Product
    	at graphql.schema.PropertyFetchingImpl.getPropertyValue(PropertyFetchingImpl.java:114)
    	at graphql.schema.PropertyDataFetcherHelper.getPropertyValue(PropertyDataFetcherHelper.java:22)
    	at graphql.schema.PropertyDataFetcher.getImpl(PropertyDataFetcher.java:131)
    	at graphql.schema.PropertyDataFetcher.get(PropertyDataFetcher.java:112)
    	at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:307)
    	... 52 common frames omitted
    Caused by: java.lang.ClassNotFoundException: models.Product
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    	... 70 common frames omitted
    

    Yet the same class CAN be found via Reflection which is the part that puzzles me.

    So I need a reproducible case showing how this is happening - why one technique fails yet reflection works in class loader terms.

    I started on a PR that makes it resilient to LinkageErrors : #3095 - but this is just working around. I have no more insight on why this is happening.

  8. SandQuattro commented on Feb 19, 2023

    @SandQuattro
    Author

    So I need a reproducible case showing how this is happening - why one technique fails yet reflection works in class loader terms.

    I started on a PR that makes it resilient to LinkageErrors : #3095 - but this is just working around. I have no more insight on why this is happening.
    Hi @bbakerman
    Here are the steps to reproduce:

    1. pull this graphql example https://github.com/manojgodara/GraphQLPlayFramework
    2. set java 8 version in project settings
    3. go to build.sbt at the root of the project, there are all project dependencies
    4. now we have to change libraryDependencies ++= Seq("com.graphql-java" % "graphql-java" % "11.0") to latest graphql version: libraryDependencies ++= Seq("com.graphql-java" % "graphql-java" % "20.0")
    5. right click on app/controllers/GraphQLController -> Run Play2 App
    6. run graphql query, for example:
      curl 'http://localhost:9000/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'Origin: altair://-' --data-binary '{"query":"query productById($id: String!) {\n productById(id: $id) {\n categoryName\n }\n}\n","variables":{"id":"1"}}' --compressed

    here is our error:
    [error] n.g.GraphQL - Execution '7d58df18-10eb-4841-8f3c-330793db87b7' threw exception when executing : query : 'query productById($id: String!) {
    productById(id: $id) {
    categoryName
    }
    }
    '. variables '{id=1}'
    java.util.concurrent.CompletionException: java.lang.NoClassDefFoundError: entity/Product
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
    at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:783)
    at java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:792)
    at java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2153)
    at graphql.execution.Execution.executeOperation(Execution.java:171)
    at graphql.execution.Execution.execute(Execution.java:102)
    at graphql.GraphQL.execute(GraphQL.java:557)
    at graphql.GraphQL.lambda$parseValidateAndExecute$11(GraphQL.java:476)
    at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995)
    Caused by: java.lang.NoClassDefFoundError: entity/Product
    at graphql.schema.PropertyFetchingImpl.getPropertyValue(PropertyFetchingImpl.java:114)
    at graphql.schema.PropertyDataFetcherHelper.getPropertyValue(PropertyDataFetcherHelper.java:22)
    at graphql.schema.PropertyDataFetcher.getImpl(PropertyDataFetcher.java:131)
    at graphql.schema.PropertyDataFetcher.get(PropertyDataFetcher.java:112)
    at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:307)
    at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:286)
    at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:212)
    at graphql.execution.AsyncExecutionStrategy.execute(AsyncExecutionStrategy.java:55)
    at graphql.execution.ExecutionStrategy.completeValueForObject(ExecutionStrategy.java:680)
    at graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:472)
    Caused by: java.lang.ClassNotFoundException: entity.Product
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at graphql.schema.PropertyFetchingImpl.getPropertyValue(PropertyFetchingImpl.java:114)
    at graphql.schema.PropertyDataFetcherHelper.getPropertyValue(PropertyDataFetcherHelper.java:22)
    at graphql.schema.PropertyDataFetcher.getImpl(PropertyDataFetcher.java:131)
    at graphql.schema.PropertyDataFetcher.get(PropertyDataFetcher.java:112)
    at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:307)
    at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:286)
    at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:212)
    [error] c.GraphQLController - Error while executing the query java.util.concurrent.CompletionException: java.lang.NoClassDefFoundError: entity/Product

    It's just a simple entity, like:
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Product {

    private Long id;
    private String categoryName;
    private String description;
    private String image;
    private String name;
    private Long parentId;
    private Collection<entity.ProductDetail> details;
    
    public Long getId() {
    	return id;
    }
    
    public void setId(Long id) {
    	this.id = id;
    }
    
    public String getCategoryName() { 
    	return categoryName;
    }
    
    @JsonSetter("parent_offering_name")
    public void setCategoryName(String categoryName) {
    	this.categoryName = categoryName;
    }
    
    public String getDescription() {
    	return description;
    }
    
    @JsonSetter("offering_description")
    public void setDescription(String description) {
    	this.description = description;
    }
    
    public String getImage() {
    	return image;
    }
    
    @JsonSetter("image_file")
    public void setImage(String image) {
    	this.image = image;
    }
    
    public String getName() {
    	return name;
    }
    
    @JsonSetter("offering_name")
    public void setName(String name) {
    	this.name = name;
    }
    
    public Long getParentId() {
    	return parentId;
    }
    
    public void setParentId(Long parentId) {
    	this.parentId = parentId;
    }
    
    public Collection<entity.ProductDetail> getDetails() {
    	return details;
    }
    
    public void setDetails(Collection<entity.ProductDetail> details) {
    	this.details = details;
    }
    
    @Override
    public boolean equals(Object obj) {
    	if (this == obj)
    		return true;
    
    	if (obj == null || !(obj instanceof Product)) {
    		return false;
    	}
    
    	Product other = (Product) obj;
    	return Objects.equals(id, other.id);
    }
    
    @Override
    public int hashCode() {
    	return HashCodeUtils.hashCode(id);
    }
    
    @Override
    public String toString() {
    	return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
    

    }

  9. bbakerman commented on Feb 22, 2023

    @bbakerman
    Member

    @SandQuattro

    This is definitely class loader based

    the object passed into the graphql-java code is a class entity.Product

    and its class loader is

    ReloadableClassLoader(v1){file:/Users/bbaker/src/graphql/repros/GraphQLPlayFramework/target/scala-2.11/classes/}
    

    but the graphql java 20 fetching code is running inside

    LambdaFetchingSupport.class.getClassLoader()

    and its of class loader type

    PlayDependencyClassLoader{file:/Users/bbaker/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.11.jar, ...
    

    eg a different class loader

    and calling LambdaFetchingSupport.class.getClassLoader().loadClass("entity.Product") fails with class not found

    And I think this is what is happening inside the java.lang.invoke.LambdaMetafactory#metafactory code - its using the class loader of the current code to get to the entity class - but the two cant see it each

    It seems that we can "reflect" and "reflect invoke" the method but we cant use the much faster LambdaMetafactory to create a call site to it with the Play framework class loaders set up like this.

    Its great I have a reproduction here - what do do about it is challenging but I can now see it happening

  10. bbakerman commented on Feb 23, 2023

    @bbakerman
    Member

    This PR is an attempt to make it more resilient

    #3095

    There is a Java 9 fix that would make this go away I think but we are likely to stay on Java 8 until April so I will leave it at this for the moment

  11. schaefa commented on Feb 24, 2023

    @schaefa
    Contributor

    I used the latest code (master) with Felix, Sling and Adobe AEM and it runs into the same issue with Pagination.
    Up and to version 19.3 it is working just fine. So far something is going wrong inside the Bundle Wiring but I don't think that is the cause. I will check with 19.3 what is the difference inside Felix but I think in 19.3 it is not obtain the class from the graphql-java class loader but the Sling bundle class loader.

  12. bbakerman commented on Feb 24, 2023

    @bbakerman
    Member

    @schaefa - can you try the branch I have posted to see if its more resilient to class loader challenges

  13. schaefa commented on Feb 28, 2023

    @schaefa
    Contributor

    @bbakerman Yes, that fixed my issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions