Telegram Bot开发报错:无法访问CompletableFuture类文件
It looks like you're hitting two key issues here: an incorrect project directory structure and a Java version mismatch. Let's break down how to fix both:
1. Correct Your Project Directory Structure
Looking at the error path:
C:\Users\safwa\Documents\IntelliJ\assignment-2-safwan0908\src\main\java\my\assignment2\src\main\java\org\example\s261251Bot.java
Your source files are nested inside a duplicate src/main/java directory, which confuses the compiler. Here's how to fix it:
- Navigate to your project folder:
assignment-2-safwan0908/src/main/java/my/assignment2/src/main/java/ - Move the
orgfolder (containing yourexamplepackage and bot files) directly into the outersrc/main/javafolder (so it becomesassignment-2-safwan0908/src/main/java/org/example/) - Delete the now-empty nested directories (
my/assignment2/src/main/java/) - Refresh your project in IntelliJ (right-click the project root > Refresh)
2. Ensure You're Using Java 8 or Higher
java.util.concurrent.CompletableFuture was introduced in Java 8. If your project is set to use an older Java version, the compiler won't find this class. Here's how to update it in IntelliJ:
- Go to File > Project Structure (or press
Ctrl+Alt+Shift+S) - Under Project Settings > Project:
- Set Project SDK to Java 8 (or a newer version like Java 11/17) – if you don't have it installed, download it from Oracle or OpenJDK first
- Set Project language level to
8 - Lambdas, type annotations etc.or higher
- Under Project Settings > Modules:
- Select your module, go to the Sources tab, and set Language level to match the project level (8+)
- If you're using Maven, add these lines to your
pom.xmlto enforce Java 8:<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> - If you're using Gradle, add this to your
build.gradle:sourceCompatibility = 1.8 targetCompatibility = 1.8
3. Clean and Rebuild the Project
After fixing the structure and Java version:
- Go to Build > Clean Project
- Then Build > Rebuild Project
- Run your
App.javaagain
These steps should resolve the missing CompletableFuture error and get your Telegram Bot running.
内容的提问来源于stack exchange,提问作者X unknown

