gson 未找到JsonReader类

gt0wga4j  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(120)

这是我第一次尝试使用java访问google sheet。我跟着谷歌教程关于如何阅读和写作。现在,我在运行google提供的代码片段时遇到了一个问题。
错误如下:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/stream/JsonReader
        at [email protected]/com.google.api.client.json.gson.GsonFactory.createJsonParser(GsonFactory.java:81)
        at [email protected]/com.google.api.client.json.JsonFactory.fromReader(JsonFactory.java:229)
        at [email protected]/com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load(GoogleClientSecrets.java:190)
        at [email protected]/com.egensolve.edt.edt.SheetController.getCredentials(SheetController.java:39)
        at [email protected]/com.egensolve.edt.edt.SheetController.main(SheetController.java:52)
    Caused by: java.lang.ClassNotFoundException: com.google.gson.stream.JsonReader
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 5 more
    
    Caused by: java.lang.ClassNotFoundException: com.google.gson.stream.JsonReader

代码没有显示任何关于Gson包的错误,所以我迷路了。
build.gradle:

plugins {
    id 'java'
    id 'application'
    id 'org.javamodularity.moduleplugin' version '1.8.12'
    id 'org.openjfx.javafxplugin' version '0.0.13'
    id 'org.beryx.jlink' version '2.25.0'
}

group 'com.egensolve.EDT'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    junitVersion = '5.9.2'
}

sourceCompatibility = '18'
targetCompatibility = '18'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'com.egensolve.edt.edt'
    mainClass = 'com.egensolve.edt.edt.Main'
}

javafx {
    version = '18.0.2'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {
    implementation('org.controlsfx:controlsfx:11.1.2')
    implementation('com.dlsc.formsfx:formsfx-core:11.6.0') {
        exclude(group: 'org.openjfx')
    }
    implementation (group: 'com.google.code.gson', name: 'gson', version: '2.7')
    implementation ('com.google.api-client:google-api-client:2.0.0')
    implementation ('com.google.oauth-client:google-oauth-client-jetty:1.34.1')
    implementation ('com.google.apis:google-api-services-sheets:v4-rev20220927-2.0.0')
    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}

test {
    useJUnitPlatform()
}

jlink {
    imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'app'
    }
}

jlinkZip {
    group = 'distribution'
}
llmtgqce

llmtgqce1#

尝试克隆整个项目并再次测试,不要忘记复制json。

git clone [email protected]:googleworkspace/java-samples.git
cd java-samples/sheets/quickstart
gradle run

在我这边工作得很好!

相关问题