Origin
When debugging some Java applications or learning the internal implementations of some library class, we need to read the source code. So I download source from some sites. Soon, it caused some inconvenience.
The class file will update whenever the package updates, now it change to:
openjdk version “1.8.0_102”
But my source code is still the old version. In the intellij idea, it will remind me that class file and source code is mismatched and has some diff. It may be no harm at first glance, but when we need to find the line where class file throws an exception, we may find an mismatched line.
Like above picture shows, we can’t following the line number to find the right line if source code and class file is mismatched.
And for the same reason, you can’t set a break point in library class file.
Solution
In order to sync the source code with class file automatically, we have to install a source package, making them updating at the same time. The following is some steps.
Find the source package
# find the source package on your linux distribution.
dnf search openjdk # or oracle or some name related
# if you want to search with regex, try following:
dnf list all | grep 'test'
# or you can try google it :)
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-headless.i686 : OpenJDK Runtime Environment
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment
java-1.8.0-openjdk-demo-debug.x86_64 : OpenJDK Demos with full debug on
…
openprops.noarch : An improved java.util.Properties from OpenJDK
thermostat.x86_64 : A monitoring and serviceability tool for OpenJDK
icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser plug-in and Web Start implementation
We can clear find a package called src
, so we move to next step.
Install
sudo dnf install java-1.8.0-openjdk-src.x86_64
It will soon install our source code package.
Find source
In this step, we are going to find where is java home, in which directory we may find our source code. Through a simple search on Internet, we can find the src.zip
in the /usr/lib/jvm/java
. But this directory is a link file. Following the link, we find the specific java version directory:
readlink -f /usr/lib/jvm/java
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-1.b14.fc24.x86_64
Set source in idea
When setting the source in the idea, we should remember to choose the /usr/lib/jvm/java
but not the specific directory. When next time the package updates, the link directory java
will redirect to new version of java, and our class and source will all be updated automatically.
Ref
Written with StackEdit.
评论
发表评论