2017-08-13 79 views
0

我在学习如何使用Spotify API,但他们的sample code不起作用。我的java代码不起作用(NoClassDefFoundError)

我正在使用Netbeans 8.1,我确实导入了.jar文件,并且在Api 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."); 
    } 

} 
+0

我的回答有帮助吗? https://stackoverflow.com/help/someone-answers – user7294900

+0

@ user7294900实际上,在我了解到有关maven的更多信息之后,我可以看到问题,但是有帮助。 –

回答

1

net/sf/json/JSON类是内部JSON-lib的JAR

在您需要添加的依赖样本:

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

检查你使用的是Maven的pom.xml的例子。

+0

我没有承认这部分,我是一个新的程序员,我该如何添加依赖关系? –

+0

阅读自述文件并使用maven或gradle进行安装https://github.com/thelinmichael/spotify-web-api-java/blob/master/README.md – user7294900

0

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

有几种方法可以纠正它。

  1. 如果是Maven项目然后添加在你的pom.xml net.sf.json-lib的依赖性:

  2. 别的坛子添加到项目lib文件夹

  3. 添加罐子类从构建配置的路径

0

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