跳至主要内容

Auto Update Java Source Code on Linux

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 this picture, you can't following the line number to find the right line if source code and class file is mismatched

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.

setting source path

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (