随着JDA 4.2.0的新更新,VPS上新建的JAR文件返回NoClassDefFoundError

问题描述

使用JDA 4.2.0的新更新尝试在VPS上运行jar文件失败。 我尝试了各种选择,例如

但是,由于VPS控制台输出保持不变,因此这些选项似乎都不起作用:

Error: Could not find or load main class ...
Caused by: java.lang.NoClassDefFoundError: net/dv8tion/jda/api/hooks/ListenerAdapter

所以我当前的问题是,我的gradle.build应该是什么样子,还是VPS无法正确刷新的问题?也许还有另一个我不知道的问题。

关于我的gradle.build是什么样的:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
    }
}

apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'

group '...'
version '2.0-SNAPSHOT'

repositories {
    mavenCentral()
    jcenter(
    )
}

dependencies {
    testCompile group: 'junit',name: 'junit',version: '4.+'
    compile 'net.dv8tion:JDA:4.2.0_168'
    compile group: 'commons-collections',name: 'commons-collections',version: '3.2'
    compile group: 'MysqL',name: 'mysql-connector-java',version: '8.0.20'
    compile group: 'javax.persistence',name: 'persistence-api',version: '1.0'
    compile 'com.vdurmont:emoji-java:5.1.1'
}

jar {
    manifest {
        attributes(
                'Main-Class': '...'
        )
    }
}

注意:在1.0-SNAPSHOT的上一个快照中,接下来的gradle.build用来启动项目:

plugins {
    id 'java'
    id 'application'
}

group '...'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    testCompile group: 'junit',version: '4.+'
    compile 'net.dv8tion:JDA:3.5.0_329'
    compile group: 'commons-collections',version: '1.0'
    compile 'com.vdurmont:emoji-java:5.1.1'
}

解决方法

您可以使用此build.gradle来建立一个有效的影子罐:

plugins {
  id 'application' // this allows you to set a mainClassName
  id 'com.github.johnrengelman.shadow' version '6.0.0' // this adds the shadowJar task
}

group '...'
version '2.0-SNAPSHOT'
mainClassName = 'your main class goes here' // this sets the main class property in your manifest automatically

repositories {
    jcenter() // jcenter is a superset of mavenCentral
}

dependencies {
    testCompile group: 'junit',name: 'junit',version: '4.+'
    compile 'net.dv8tion:JDA:4.2.0_191'
    compile group: 'commons-collections',name: 'commons-collections',version: '3.2'
    compile group: 'mysql',name: 'mysql-connector-java',version: '8.0.20'
    compile group: 'javax.persistence',name: 'persistence-api',version: '1.0'
    compile 'com.vdurmont:emoji-java:5.1.1'
}

运行shadowJar gradle任务来编译jar文件。然后将其放置在build/libs内,格式为[name]-all.jar。别忘了用您的主类的完全限定名称替换mainClassName