You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spotify Web API时遭遇ClassNotFoundException问题求助

问题:运行Spotify API Java代码时出现ClassNotFoundException

我正在开发一个基于Spotify Web API的数据插件,写了同步和异步获取艺人信息的Java代码,但运行时抛出ClassNotFoundException,提示找不到org.apache.hc.client5.http.impl.cache.CacheConfig类。我已经在VSCode项目的引用库中添加了Jar包,附上代码、错误堆栈和POM文件,求排查原因。

代码

import se.michaelthelin.spotify.SpotifyApi;
import se.michaelthelin.spotify.exceptions.SpotifyWebApiException;
import se.michaelthelin.spotify.model_objects.specification.Artist;
import se.michaelthelin.spotify.requests.data.artists.GetArtistRequest;
import org.apache.hc.core5.http.ParseException;
import com.google.gson.JsonSyntaxException;

import java.io.IOException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Future;

public class plugin {
    // 所有请求都需要访问令牌
    private static final SpotifyApi spotifyApi = new SpotifyApi.Builder()
            .setAccessToken("xxx")
            .build();

    private static final GetArtistRequest getArtistRequest = spotifyApi.getArtist("qKRpDADUKrFeKhFHDMdfcu")
            .build();

    public static void getArtist_Sync() {
        try {
            // 同步执行请求
            final Artist artist = getArtistRequest.execute();

            // 打印艺人名称
            System.out.println("Name: " + artist.getName());
        } catch (Exception e) {
            System.out.println("Something went wrong!\n" + e.getMessage());
        }
    }

    public static void getArtist_Async() {
        try {
            // 异步执行请求
            final Future<Artist> somethingFuture = getArtistRequest.executeAsync();

            // 执行其他操作...

            // 等待请求完成
            final Artist something = somethingFuture.get();

            // 打印艺人名称
            System.out.println("Name: " + something.getName());
        } catch (Exception e) {
            System.out.println("Something went wrong!\n" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        getArtist_Sync();
        getArtist_Async();
    }
}

错误堆栈信息

at se.michaelthelin.spotify.SpotifyHttpManager.<init>(SpotifyHttpManager.java:60)
at se.michaelthelin.spotify.SpotifyHttpManager$Builder.build(SpotifyHttpManager.java:392)
at se.michaelthelin.spotify.SpotifyApi.<clinit>(SpotifyApi.java:77)
at se.michaelthelin.spotify.SpotifyApi$Builder.<init>(SpotifyApi.java:1807)
at src.main.java.edu.cmu.plugin.<clinit>(plugin.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.hc.client5.http.impl.cache.CacheConfig
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        ... 5 more

POM文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>edu.cmu.cs214</groupId>
  <artifactId>backend</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>backend</name>
  <!-- FIXME 替换为项目实际网站 -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.12.5</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.8</version>
    </dependency>
    <dependency>
      <groupId>com.neovisionaries</groupId>
      <artifactId>nv-i18n</artifactId>
      <version>1.29</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents.client5</groupId>
      <artifactId>httpclient5-cache</artifactId>
      <version>5.1</version>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.10.19</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.25</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>se.michaelthelin.spotify</groupId>
      <artifactId>spotify-web-api-java</artifactId>
      <version>8.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents.client5</groupId>
      <artifactId>httpclient5-cache</artifactId>
      <version>5.2.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.8</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- 锁定插件版本,避免使用Maven默认版本(可移至父POM) -->
      <plugins>
        <!-- clean生命周期 -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- 默认生命周期,Jar打包 -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site生命周期 -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.36</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>
解决方案

问题根源

  1. 重复依赖冲突:POM中重复声明了httpclient5-cache(5.1/5.2.1)、gson、junit,Maven依赖解析混乱;手动添加Jar包和Maven依赖版本不一致,引发类加载冲突。
  2. Java版本不兼容:指定的编译版本是Java 1.7,但Spotify API 8.0.0依赖的Apache HttpClient 5.x最低要求Java 8,版本不匹配导致类加载失败。
  3. 依赖管理混乱:同时用Maven和手动添加Jar包,类加载器无法正确识别所需类。

修复步骤

  1. 清理重复依赖:删除POM中重复的httpclient5-cache、gson、junit,保留单一版本:
    • 保留httpclient5-cache 5.2.1(与Spotify API依赖版本匹配)
    • 保留junit 4.13.2(较新版本)
    • 保留gson 2.8.8一次
  2. 升级Java编译版本:修改POM中的编译版本为Java 8或更高:
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
  3. 移除手动添加的Jar包:完全依赖Maven管理依赖,删除VSCode中手动添加的Jar包,执行mvn clean install重新构建依赖。
  4. 验证依赖树:执行mvn dependency:tree确认httpclient5-cache已正确引入,无版本冲突。

验证修复

执行mvn clean compile exec:java -Dexec.mainClass="edu.cmu.plugin"运行代码,确认异常消失。

内容的提问来源于stack exchange,提问作者efang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 23:27:01