SamJakob / SpiGUI

A comprehensive GUI API for Spigot with pages support.
MIT License
110 stars 22 forks source link

ClassNotFound error with Gradle #26

Closed JamieIsGeek closed 1 year ago

JamieIsGeek commented 1 year ago

I'm having an issue where when I try to load the plugin on a server I get this error "java.lang.NoClassDefFoundError: com/samjakob/spigui/SpiGUI" Here is the implementation in the build.gradle file:

repositories {
    mavenCentral()
    maven {
        name = "spigotmc-repo"
        url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
    }
    maven {
        name = "sonatype"
        url = "https://oss.sonatype.org/content/groups/public/"
    }
    maven { url 'https://jitpack.io' }
}

dependencies {
    compileOnly "org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT"
    compileOnly "org.xerial:sqlite-jdbc:3.8.11.2"
    compileOnly 'com.zaxxer:HikariCP:2.3.2'
    compileOnly "com.samjakob:SpiGUI:v1.3"
}

Any help would be lovely :D

SamJakob commented 1 year ago

Hey,

I think the problem is that SpiGUI is not being included in your plugin (however it is designed to be and normally is, although it could be included in a standalone plugin).

The simplest fix should just be to change compileOnly to compile for the SpiGUI dependency.

FYI: compileOnly is for dependencies whose API is required at compile time but whose implementation is to be provided by a consuming library, application or runtime environment. compile provides the API and the implementation (i.e., at compile time and at runtime) - there is also implementation which is only at runtime.

JamieIsGeek commented 1 year ago

Unfortunately compile isn't a method acoording to my gradle build error and using implementation provides the same error as the initial issue

SamJakob commented 1 year ago

Ah, seems they’ve finally removed compile from Gradle 7+ so I think I need to revisit my Gradle classpath knowledge. This site says that they’re now compileOnly, runtimeOnly, and implementation (where implementation is both compile and runtime).

In any case implementation shouldn’t have had the same error - are you sure the Gradle configuration was refreshed?

JamieIsGeek commented 1 year ago

Yep 100% sure it was refreshed

SamJakob commented 1 year ago

Strange… I’ve not run into this myself, I’ll try making a minimal test with the latest versions.

JamieIsGeek commented 1 year ago

Any update on this?

SamJakob commented 1 year ago

Yep, managed to reproduce the issue and solve it. implementation tells Gradle it should be included in the runtime classpath, but we also need to shade SpiGUI into the JAR. This can be done by adding the following to your build.gradle:

jar {
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}