我正在为插头服务器编写插件,我需要访问Google表格上的一些数据。我几乎逐字逐句地复制了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'
}
如果有人可以帮助我了解正在发生的事情,那就太好了。对于大量的代码转储,我们也感到抱歉。
答案 0 :(得分:0)
对于那些感兴趣的人,我给出了答案。基本上,问题是我正在编译的jar在编译时出现,而不是在运行它们时出现。我将其添加到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。