使用 DropBox Core 时出现 NoClassDefFoundError API
NoClassDefFoundError while using DropBox Core API
我从官方 DropBox API 教程中复制粘贴了以下代码。这段代码的目的是上传一个文本文件。我正在使用 DropBox-core-sdk-3.0.3.jar
。但是我得到
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonParseException
at com.dropbox.core.DbxHost.<clinit>(DbxHost.java:114)
at com.dropbox.core.v2.DbxClientV2.<init>(DbxClientV2.java:31)
at dropboxtest.Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
现在解决这个问题的方法可能是,将 jackson-annotations.jar
添加到 class 路径但是,我已经尝试过了,但这似乎不起作用,我得到了同样的异常,我有一些未使用的导入
代码如下:
package dropboxtest;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;
import com.dropbox.core.v2.files.ListFolderResult;
import com.dropbox.core.v2.files.Metadata;
import com.dropbox.core.v2.users.FullAccount;
import java.util.List;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
public class Main {
private static final String ACCESS_TOKEN = "XXXXXX";
public static void main(String args[]) throws DbxException, IOException {
// Create Dropbox client
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial", "en_US");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
// Get current account info
FullAccount account = client.users().getCurrentAccount();
System.out.println(account.getName().getDisplayName());
// Get files and folder metadata from Dropbox root directory
ListFolderResult result = client.files().listFolder("");
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
// Upload "test.txt" to Dropbox
try (InputStream in = new FileInputStream("test.txt")) {
FileMetadata metadata = client.files().uploadBuilder("/test.txt")
.uploadAndFinish(in);
}
}
}
简答:使用 jackson-core-x.x.x
而不是 jackson-annotation-xxx
长 Answer:As Gemini Keith 指出,问题出在我的类路径上。 dropbox-core-sdk-3.0.3.jar
实际上需要另一个库 jackson-core-2.6.1
。但是,DropBox 的 API v2 官方文档中没有提到这一点。但是在查看 DropBox 的 API v1 文档,并阅读
行时
The necessary JAR files are in the "lib/" folder.
我开始下载 API v1 示例代码。然后将 jackson-core-x.x.x
从 lib 文件夹添加到我的当前类路径和 VIOLA 一切正常。
我从官方 DropBox API 教程中复制粘贴了以下代码。这段代码的目的是上传一个文本文件。我正在使用 DropBox-core-sdk-3.0.3.jar
。但是我得到
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonParseException
at com.dropbox.core.DbxHost.<clinit>(DbxHost.java:114)
at com.dropbox.core.v2.DbxClientV2.<init>(DbxClientV2.java:31)
at dropboxtest.Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
现在解决这个问题的方法可能是,将 jackson-annotations.jar
添加到 class 路径但是,我已经尝试过了,但这似乎不起作用,我得到了同样的异常,我有一些未使用的导入
代码如下:
package dropboxtest;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;
import com.dropbox.core.v2.files.ListFolderResult;
import com.dropbox.core.v2.files.Metadata;
import com.dropbox.core.v2.users.FullAccount;
import java.util.List;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
public class Main {
private static final String ACCESS_TOKEN = "XXXXXX";
public static void main(String args[]) throws DbxException, IOException {
// Create Dropbox client
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial", "en_US");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
// Get current account info
FullAccount account = client.users().getCurrentAccount();
System.out.println(account.getName().getDisplayName());
// Get files and folder metadata from Dropbox root directory
ListFolderResult result = client.files().listFolder("");
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
// Upload "test.txt" to Dropbox
try (InputStream in = new FileInputStream("test.txt")) {
FileMetadata metadata = client.files().uploadBuilder("/test.txt")
.uploadAndFinish(in);
}
}
}
简答:使用 jackson-core-x.x.x
而不是 jackson-annotation-xxx
长 Answer:As Gemini Keith 指出,问题出在我的类路径上。 dropbox-core-sdk-3.0.3.jar
实际上需要另一个库 jackson-core-2.6.1
。但是,DropBox 的 API v2 官方文档中没有提到这一点。但是在查看 DropBox 的 API v1 文档,并阅读
The necessary JAR files are in the "lib/" folder.
我开始下载 API v1 示例代码。然后将 jackson-core-x.x.x
从 lib 文件夹添加到我的当前类路径和 VIOLA 一切正常。