kotlinx / ast

Generic AST parsing library for kotlin multiplatform
Apache License 2.0
325 stars 23 forks source link

java.lang.NoClassDefFoundError: Could not initialize class kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParser #34

Closed ghost closed 3 years ago

ghost commented 3 years ago

My project is a Gradle Plugin that parses Kotlin code to generate flat files.

I am trying to replace kastree with this library, but i encounter the captioned error when I run my plugin in another project. My code works fine within my Gradle plugin project and my tests though. I suppose this is a missing transitive dependency problem, but I can't figure out what is missing exactly.

In my Gradle plugin project I have the following dep:

api 'com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm:a96e681f90'

Note that the Gradle project uses subprojects. The parsing is implemented in one of the subproject, and the Gradle plugin project has an implementation dependency on that subproject. I also tried with an api dependency to no avail.

Should i add a runtime dependency on the kotlin parser ?

ghost commented 3 years ago

I realize this may not be directly an issue of the library, but a consequence of using the ShadowJar plugin.

Note that i have the following file in my generated jar:

kotlinx/ast/grammar/kotlin/target/antlr/kotlin/KotlinGrammarAntlrKotlinParser.class
drieks commented 3 years ago

Hi @gauthier-roebroeck-mox, when the class is not found (for example, if you remove the .class file from the jar file) you will get a ClassNotFoundException. NoClassDefFoundError means that there was an exception while running the static initialisation code of the given class. There should be another error in the log before this. This can be a ClassNotFoundException, caused by one of the dependencies not found.

can you send me a list of all kotlinx.ast and org.antlr file in yours jar file? maybe like this:

unzip -l jarfile.jar | grep kotlinx
unzip -l jarfile.jar | grep ast
ghost commented 3 years ago

@drieks here is a gist with the 2 greps you provided. Appreciate if you can help finding out what's missing for your library to function.

ghost commented 3 years ago

There should be another error in the log before this

There's no other error prior to that one.

Here is the code I use to parse:

fun parseFromStream(input: InputStream, properties: PropertiesProvider): List<Pair<String, List<Schema>>> {
        val source = AstSource.String("", input.bufferedReader().readText())
        val kotlinFile = KotlinGrammarAntlrKotlinParser.parseKotlinFile(source)
        ...
}
drieks commented 3 years ago

Hi @gauthier-roebroeck-mox,

sadly it is very hard to help in this case because I can't debug it. Can you create a repository containing only the files required to reproduce this problem?

ghost commented 3 years ago

Can you create a repository containing only the files required to reproduce this problem?

That may be difficult, it is an internal private project for my employer, i cannot share the code as is.

drieks commented 3 years ago

Hi @gauthier-roebroeck-mox,

you can start with an empty project/directory and add only the required files, mainly the build file. No additional dependencies (expect kotlinx.ast) are required, only one small example main and the important part, the jar building settings. No other source files are required. This is called "minimal working example" and allows me to debug this problem.