NoClassDefFoundError for 3rd party scala dependencies
ControlNet opened this issue ยท 1 comments
Hi,
I'm porting my mod to 1.18 with Forge 40.1.0, but have problem of NoClassDefFoundError
for 3rd party scala library.
Reproduce steps:
- Use your SLP-example repository.
- Add random scala dependencies to gradle. For example,
implementation(group: "org.json4s", name: "json4s-native_2.13", version: "4.1.0-M1")
- Use this library in
ExampleMod.scala
as below.
package com.example.examplemod
import net.minecraftforge.fml.common.Mod
import org.apache.logging.log4j.LogManager
import org.json4s.native.JsonMethods._
@Mod(ExampleMod.MOD_ID)
object ExampleMod {
final val MOD_ID = "examplemod"
private val LOGGER = LogManager.getLogger
LOGGER.info(parse(""" { "numbers" : [1, 2, 3, 4] } """))
}
- Run gradle
runClient
, and it will throwSuppressed: java.lang.NoClassDefFoundError: org/json4s/native/JsonMethods$
I tested with multiple external scala libraries and did some experiments to solve it.
- Copy-paste the source code of external libraries to the local project. It will be load successfully.
- Build Jar shadowing the external libraries, and it will be OK.
But these solutions aren't good to use in development. Do you have any better idea to deal with it?
See this example from kotlin loader.
https://github.com/thedarkcolour/KotlinForForge/blob/site/thedarkcolour/kotlinforforge/gradle/kff-3.3.2.gradle#L18-L28
https://github.com/thedarkcolour/KotlinForForge/blob/site/thedarkcolour/kotlinforforge/gradle/kff-3.3.2.gradle#L44-L48
Create new configuration and add libraries to it. Then add them to runtime classpath as the example shows in line 24-28.