How to Read Java Source Code in IntelliJ IDEA: Complete Navigation Guide
Purpose
This post demonstrates how to view and navigate Java source code in IntelliJ IDEA. When I try to understand how libraries work internally, I often hit a wall because I can’t see the source code.
Environment
- Java 21
- IntelliJ IDEA 2024.1
- Spring Boot 3.2.x
- Maven 3.9.x
The Problem
When I debug Spring Framework code, I get stuck when I try to see what’s happening inside the library. For example:
@RestControllerpublic class UserController { @Autowired private UserService userService; // Ctrl+B doesn't work - just shows compiled class}When I press Ctrl+B on UserService, IntelliJ opens the bytecode instead of the source code. This makes debugging and learning from open source projects difficult.
What I tried first
I tried pressing Ctrl+B everywhere, but it didn’t work for external libraries. I thought maybe IntelliJ had a setting I missed.
I checked my project structure:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>3.2.1</version> </dependency></dependencies>The dependencies are there, but when I Ctrl+B on Spring classes, I get this error in the editor:
Cannot find declaration to go toHow to solve it
I figured out the issue: the source code for external libraries isn’t automatically downloaded. Here’s what I did:
1. Download sources manually
First, I went to File → Project Structure → Libraries. I found the Spring libraries in the list and clicked on one:
Spring Boot :: spring-boot-starter-web (3.2.1)I clicked the “Download Sources” button. IntelliJ downloaded the source JAR files.
After downloading sources, I pressed Ctrl+B on UserService again, and this time it opened:
// Spring Framework UserService implementationpublic class UserService { // Now I can see the actual implementation public User createUser(User user) { // Business logic I wanted to understand return userRepository.save(user); }}2. Enable automatic source downloading
I didn’t want to download sources every time, so I enabled automatic download:
- File → Settings → Appearance & Behavior → System Settings
- Check “Download source files automatically”
Now when I add new dependencies, IntelliJ automatically downloads the sources.
3. Maven source JARs
For Maven projects, I also added this plugin to ensure source JARs are built:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins></build>Navigation techniques I use now
Basic navigation
Ctrl+B/Cmd+B: Go to declaration (my most used shortcut)Ctrl+Alt+B/Cmd+Option+B: Go to implementationsF4: Go to source (open in editor)Shift+F4: Open in new tab
When I work on Spring projects, I use these constantly to understand the framework.
Advanced features
Ctrl+Shift+I: Quick documentation lookupCtrl+Q: Quick documentation popupAlt+F7: Find usagesCtrl+Alt+F7: Find usages in file
These help me understand how classes are used across the codebase.
Structure window
I also use the Structure window (Alt+7) to get an overview of a class:
public class ExampleClass { // Fields shown in Structure private String field1;
// Methods listed with signatures public void method1() {} public String method2() { return "result"; }}Why this matters
Understanding source code helps me in several ways:
- Learning from open source: I can see how Spring Framework handles dependency injection
- Debugging complex issues: When a bug occurs, I can trace through the library code
- Improving code quality: I study patterns from production libraries
- Understanding framework internals: Knowing how @Autowired works behind the scenes
Common mistakes I made
- Not having sources downloaded: This was my main issue
- Using only basic navigation: I missed advanced features like finding usages
- Forgetting about the Structure window: It gives great context
- Not leveraging IntelliJ’s documentation: Sometimes docs are better than source
Summary
In this post, I showed how to view Java source code in IntelliJ IDEA. The key point is enabling source downloading and using Ctrl+B navigation. Now when I work with Spring Framework or other libraries, I can explore the actual source code to understand how things work.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments