Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ shows decompiled .class file instead of source code

I am having issues with IntelliJ - it shows me a "decompiled" version of the class instead of its source code if I ctrl+click the class.

This is what I see:

Decompiled class message

I am trying to open regular JDK source files.

The most relevant issue I found is explained here: Java sources replaced by decompiled files in Intellij

The most detailed answer I've found so far is there as well.

Yet it doesn't work for me:

  1. Bytecode Viewer plugin is off.

  2. Java Bytecode Decompiler plugin is off.

  3. Platform, Project and Module all have same JDK version setup.

  4. I have two JDKs available in IntelliJ: jdk1.8.0_161 and jbsdk8u112b287.2_windows_x86 (bundled with IntelliJ, but comes without src.zip). Tried with both (had to specify src.zip of jdk1.8.0_161 for jbsdk8u112b287.2_windows_x86) - no luck

  5. src.zip is not corrupt - I can open it via WinRAR

  6. When added jdk1.8.0_161 to IntelliJ, it automatically found src.zip. When manually adding src.zip to jbsdk8u112b287.2_windows_x86 Sourcepath tab - it started "Scanning for roots", but warned me that it "cannot determine what kind of files the chosen items contain" and offered me to "attach them as 'Sources'" so I agreed.

  7. Tried File > Invalidate Caches / Restart... - same result.

  8. Same issue in 2017.3 and fresh 2018.1 versions of IntelliJ.

  9. Providing path for Documentation makes IntelliJ look for the documentation in the path specified. So it works as it should for both of JDKs. But not for sources.

  10. Everything else works/compiles properly.

My settings:

Settings

like image 564
takeoff Avatar asked Mar 29 '18 13:03

takeoff


People also ask

What is a decompiled .class file?

A Java Decompiler is a special type of decompiler which takes a class file as input and produces Java source code as output. The decompilation is exactly the reverse process of compilation. Thus, decompiler does not produce a replica of the source code.

How can I view source code in IntelliJ?

From the main menu, select View | Show Siblings. IntelliJ IDEA opens a popup where you can browse through the implementations, navigate to source, edit code, and open the list in the Find tool window.

How do I open a .class file in IntelliJ?

For the newer versions of Intellij, press Ctrl + Alt + Shift + s to open project settings, then choose Modules, under Dependencies tab choose the green + and add the folder in which your . class file is located.


2 Answers

1. Get the source files

Make sure you have downloaded and installed the source files of the JDK.

I'm on Arch Linux and installed them with pacman -S openjdk8-src which put a src.zip into /usr/lib/jvm/java-8-openjdk/. We'll use that zip file in the next step.

To list source packages of different JDK versions, do pacman -Ss "openjdk.*-src".

See this question on how to get the source files for your specific platform.

2. Point IntelliJ to the source files

In your module settings (open them with F4) under "Platform Settings" → "SDKs" → "Sourcepath", add the zip file containing the JDK source to your project JDK (be sure to pick the right JDK if IntelliJ lists more than one JDK) using the button with the plus sign:

Sourcepath screenshot

3. Jump into the source

Select a JDK class, like Comparator, using IntelliJ's Search Everywhere or Ctrl+b with the cursor on the name of the class.

Comparator source

like image 159
Matthias Braun Avatar answered Sep 19 '22 11:09

Matthias Braun


I also have the same problem. The problem for my case is that Project SDK is set to 13 [Invalid]. After I can Project SDK to a valid SDK. My problem is solved.

Project Structure

like image 26
Jingguo Yao Avatar answered Sep 22 '22 11:09

Jingguo Yao