我的java代码不工作(NoClassDefFoundError)

我的java代码不工作(NoClassDefFoundError),java,api,spotify,Java,Api,Spotify,我正在尝试学习如何使用SpotifyAPI,但他们的API不起作用 我使用的是Netbeans 8.1,我导入了.jar文件,在Api=Api.builder()行中显示java.lang.NoClassDefFoundError:net/sf/json/json import com.wrapper.spotify.Api; import com.wrapper.spotify.methods.AlbumRequest; import com.wrapper.spotify.models.Al

我正在尝试学习如何使用SpotifyAPI,但他们的API不起作用

我使用的是Netbeans 8.1,我导入了.jar文件,在
Api=Api.builder()
行中显示
java.lang.NoClassDefFoundError:net/sf/json/json

import com.wrapper.spotify.Api;
import com.wrapper.spotify.methods.AlbumRequest;
import com.wrapper.spotify.models.Album;
import java.util.List;

public static void main(String[] args) {

    // Create an API instance. The default instance connects to https://api.spotify.com/.
    Api api = Api.builder()
            .clientId("<secret>")
            .clientSecret("<secret>")
            .redirectURI("<secret>")
            .build();

    // Create a request object for the type of request you want to make
    AlbumRequest request = api.getAlbum("7e0ij2fpWaxOEHv5fUYZjd").build();

    // Retrieve an album
    try {
        Album album = request.get();

        // Print the genres of the album
        List<String> genres = album.getGenres();
        for (String genre : genres) {
            System.out.println(genre);
        }
    } catch (Exception e) {
        System.out.println("Could not get albums.");
    }

}
import com.wrapper.spotify.Api;
导入com.wrapper.spotify.methods.AlbumRequest;
导入com.wrapper.spotify.models.Album;
导入java.util.List;
公共静态void main(字符串[]args){
//创建API实例。默认实例连接到https://api.spotify.com/.
Api=Api.builder()
.clientId(“”)
.clientSecret(“”)
.URI(“”)
.build();
//为要发出的请求类型创建请求对象
AlbumRequest-request=api.getAlbum(“7E0IJ2FPWPWAXOEHV5FUYZJD”).build();
//检索相册
试一试{
Album Album=request.get();
//打印相册的类型
List-genres=album.getGenres();
对于(字符串类型:类型){
System.out.println(体裁);
}
}捕获(例外e){
System.out.println(“无法获取相册”);
}
}

net/sf/json/json
类位于json库jar中

在示例中,您需要添加其依赖项:

 <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>

net.sf.json-lib

在示例中。

这些可能是原因: 1.Java虚拟机无法在运行时找到编译时可用的特定类。 2.如果类在编译时存在,但在运行时java类路径中不可用

有几种方法可以纠正它

  • 如果是maven项目,则在pom.xml中添加net.sf.json-lib依赖项:

  • else将jar添加到项目库文件夹

  • 从构建配置将jar添加到类路径


  • 如果是你的maven项目,那么在pom.xml中添加依赖项,或者你可以下载外部jar并附加到项目中。

    我不理解这一部分,我是一名新程序员,我如何添加依赖项?阅读自述文件并使用maven或gradle安装我的答案有帮助吗@用户7294900事实上,在我了解了更多关于maven的知识后,我可以看到这个问题,但确实有帮助。