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

Java开发Telegram Bot启动失败:找不到CompletableFuture类

问题:Telegram Bot启动报错java.util.concurrent.CompletableFuture找不到

我通过Maven引入Telegram Bot开发库,编写了如下启动代码:

import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

public class Main {
    public static void main (String [] args) {
        try {
            TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
            botsApi.registerBot(new telegaBot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

Bot逻辑代码:

import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

public class telegaBot extends TelegramLongPollingBot {
    @Override
    public String getBotUsername() {
        return "bot_name";
    }

    @Override
    public String getBotToken() {
        return "token";
    }

    @Override
    public void onUpdateReceived(Update update) {
    }
}

启动时出现错误:

java: cannot access java.util.concurrent.CompletableFuture class file for java.util.concurrent.CompletableFuture not found

我的环境版本:

  • Java版本:1.7
  • Maven版本:3.6.3
  • IDEA版本:IntelliJ IDEA 2021.1.3(社区版)
解决方案

这个错误的核心原因是**CompletableFuture是Java 8及以上版本才引入的类**,而当前项目使用的Java 1.7不支持该类。Telegram Bot开发库的新版本依赖Java 8+环境,因此需要将项目的Java版本升级到8或更高。

具体操作步骤:

  1. 修改Maven的pom.xml配置,指定Java编译版本为1.8:
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
  1. 同步IDEA的项目设置:

    • 打开File > Project Structure(快捷键Ctrl+Alt+Shift+S)
    • 在Project Settings > Project中,将Project SDK选择为Java 8及以上版本,Project language level设置为8 - Lambdas, type annotations etc.
    • 在Project Settings > Modules的Sources标签页,将对应模块的Language level设置为8
  2. 执行mvn clean install重新构建项目,再启动Bot即可解决该问题。

内容的提问来源于stack exchange,提问作者Денис

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 09:20:29