1

I have a big problem :

I want to make a very simple Telegram Bot .


and I've searched a lot of websites and I found this code but I got this error when running following code :

I tried in Eclipse but the error is same and it's about ClassLoader


I don't know what is the problem and what to do ?

C:\Users\HM\.jdks\openjdk-16.0.2\bin\java.exe "-javaagent:E:\IntelliJ IDEA 
Community Edition 2021.1.1\lib\idea_rt.jar=51246:E:\IntelliJ IDEA Community Edition 
2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath 

C:\Users\HM\IdeaProjects\telegramNew\out\production\telegramNew
;C:\Users\HM\Desktop\telegram 
bots-5.3.0.jar;C:\Users\HM\Desktop\telegrambots-meta-5.3.0.jar com.company.Main
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
at com.company.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 1 more

My main method:

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 MyBot());
    } catch (TelegramApiException e) {
      e.printStackTrace();
    }

  }
}

My Bot :

import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

public class MyBot extends TelegramLongPollingBot {

  @Override
  public String getBotUsername() {
    return "hamidComputerBot";
  }

  @Override
  public String getBotToken() {
    return "1909766145:AAF7rk22xEbtM45zUm4RVCpK3MNpjuD52lI";
  }

  @Override
  public void onUpdateReceived(Update update) {

    if (update.hasMessage() && update.getMessage().hasText()) {
      SendMessage message = new SendMessage(); // Create a SendMessage object with 
      mandatory fields
      message.setChatId(update.getMessage().getChatId().toString());
      message.setText(update.getMessage().getText());

      try {
        execute(message); // Call method to send the message
      } catch (TelegramApiException e) {
        e.printStackTrace();
      }

    }
  }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
hamid
  • 47
  • 5

1 Answers1

2

I had the same trouble.
You should import maven lib with transitive dependencies (look below how it looks in Intellij Idea).
It required because some parts of telegram Api use this classes which is not included in telegram bots-5.3.0.jar

enter image description here