Marcus Rigonati
Marcus Rigonati

Reputation: 25

My java code is not working (NoClassDefFoundError)

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

}

Upvotes: 0

Views: 137

Answers (3)

Amardeep Kumar
Amardeep Kumar

Reputation: 1

If it's your maven project then add the dependecy in pom.xml or you can download the external jar and attach into the project.

Upvotes: 0

user2862544
user2862544

Reputation: 425

these could be the reasons: 1.Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. 2. If a class was present during compile time but not available in java classpath during runtime.

there are several ways rectify it.

  1. if it is maven project then add net.sf.json-lib dependency in your pom.xml:

  2. else add jar to project lib folder

  3. add the jar to class path from build configuration

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58822

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.

Upvotes: 1

Related Questions