【问题标题】:My java code is not working (NoClassDefFoundError)我的 java 代码不工作(NoClassDefFoundError)
【发布时间】:2017-08-13 13:59:42
【问题描述】:

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

我使用的是 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.");
    }

}

【问题讨论】:

  • 我的回答有帮助吗? stackoverflow.com/help/someone-answers
  • @user7294900 实际上,在我了解更多有关 maven 的信息后,我可以看到问题所在,但确实有帮助。

标签: java api spotify


【解决方案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

【讨论】:

【解决方案2】:

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

有几种方法可以纠正它。

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

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 2012-01-30
      • 2016-11-15
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多