My java code is not working (NoClassDefFoundError)

时间:2017-08-13 13:59:43

标签: java api spotify

I'm trying to learn how to use the Spotify API, but their sample code is not working.

I'm using Netbeans 8.1, I did import the .jar files and it's saying java.lang.NoClassDefFoundError: net/sf/json/JSON in the Api api = Api.builder() line.

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.");
    }

}

3 个答案:

答案 0 :(得分:1)

net/sf/json/JSON class is inside json-lib jar

In the sample you need to add its dependency:

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

Check that you are using the maven's pom.xml in the example.

答案 1 :(得分:0)

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

有几种方法可以纠正它。

  1. 如果是maven项目,那么在你的pom.xml中添加net.sf.json-lib依赖:

  2. 否则将jar添加到项目lib文件夹

  3. 将jar添加到构建配置的类路径

答案 2 :(得分:0)

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