【问题标题】:java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory with Google Sheets Examplejava.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory 与 Google 表格示例
【发布时间】:2019-02-06 16:18:45
【问题描述】:

我正在为 spigot 服务器编写插件,我需要访问 Google 表格上的一些数据。我几乎逐字复制了谷歌快速入门page。唯一真正的区别是我把它放在另一个类中并调用了主函数TestGet。我还稍微更改了 build.gradle 以匹配我的项目结构。当我运行插件时,我得到 JsonFactory 的 NoClassDefFoundError。我很确定我没有丢失依赖项,因为我已将依赖项包含在 gradle 中。

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class SheetsInterface {

    private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    // Load client secrets.
    InputStream in = SheetsInterface.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline")
            .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}

public static void TestGet() throws IOException, GeneralSecurityException
{
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    final String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
    final String range = "Class Data!A2:E";
    Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
            .setApplicationName(APPLICATION_NAME)
            .build();
    ValueRange response = service.spreadsheets().values()
            .get(spreadsheetId, range)
            .execute();
    List<List<Object>> values = response.getValues();
    if (values == null || values.isEmpty()) {
        System.out.println("No data found.");
    } else {
        System.out.println("Name, Major");
        for (List row : values) {
            // Print columns A and E, which correspond to indices 0 and 4.
            System.out.printf("%s, %s\n", row.get(0), row.get(4));
        }
    }
}
}

这是错误本身

java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory
    at net.projectName.Main.onEnable(Main.java:17) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:254) ~[spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugin(CraftServer.java:434) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugins(CraftServer.java:348) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at net.minecraft.server.v1_13_R2.MinecraftServer.l(MinecraftServer.java:580) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:542) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:420) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at net.minecraft.server.v1_13_R2.DedicatedServer.init(DedicatedServer.java:294) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:698) [spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
Caused by: java.lang.ClassNotFoundException: com.google.api.client.json.JsonFactory
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_191]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:134) ~[spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:80) ~[spigot-1.13.2.jar:git-Spigot-f56e2e7-b634e05]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_191]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_191]
    ... 12 more

这里是 build.gradle:

apply plugin: "java"

sourceSets
{
    main.java.srcDir      "src/main/java"
    test.java.srcDir      "src/test/java"
    main.resources.srcDir "src/main/resources"
}

repositories {
     mavenCentral()
}

dependencies
{
    compile files("libs/spigot-api-1.13.2-R0.1-SNAPSHOT-shaded.jar")
    compile 'com.google.api-client:google-api-client:1.23.0'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
    compile 'com.google.apis:google-api-services-sheets:v4-rev516-1.23.0'
}

如果有人可以帮助我了解发生了什么,那就太好了。也为大量的代码转储感到抱歉。

【问题讨论】:

  • 您可能会发现这很有用stackoverflow.com/questions/17231722/…
  • 我在 build.gradle 中添加了 compile " 'com.google.http-client:google-http-client-jackson:1.15.0-rc' " ,结果还是一样跨度>
  • 添加到我的其他评论中,它有点工作,但现在有时它会给出 java.lang.NoClassDefFoundError: com/google/api/client/http/HttpRequestInitializer 代替

标签: java gradle google-sheets runtime-error


【解决方案1】:

对于那些感兴趣的人,我想出了答案。问题基本上是我正在编译的罐子在编译时就在附近,但在我运行它们时却没有。我将此添加到 build.gradle 文件中:

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'ProjectName',  
            'Implementation-Version': "1.0"
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

然后,当您想创建 Jar 时,可以调用“gradle fatJar”,而不是调用“gradle build”。这将在 jar 文件中包含 jar(稍微简化),以便 jar 在运行时可以找到它们。我发现了如何做到这一点here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2011-07-01
    • 2012-06-01
    相关资源
    最近更新 更多