2017-08-13 71 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並附加到項目中。