How to Open Java Source Code in Eclipse

Opening Eclipse can be a tiny bit tricky if you don’t know how to do it. This tutorial will teach you how to open your Java Source Code in Eclipse if you already have a program running, if you have opened a file by yourself and you have not.

This article explains how to open a Java source code file in Eclipse. It describes how to navigate to the file and open it in the Java editor window or in the properties window for a project. The instructions assume you are using Eclipse IDE for Java Developers, version 4.7.0, and that you have a file that ends with .java extension.

Top 30 Eclipse Keyboard Shortcuts for Java Programmer.

1) Ctrl + Shift + T for finding class even from jar

This keyboard shortcut in Eclipse is my most used and favorite shortcut. While working with a high-speed trading system which has a complex code, I often need to find classes with the just blink of the eye and this Eclipse keyboard shortcut is just made for that. No matter whether you have class in your application or inside any JAR, this shortcut will find it.

2) Ctrl + Shift + R for finding any resource (file) including config XML files

This is similar to above Eclipse shortcut with the only difference that it can find out not only Java files but any files including XML, configs, and many others, but this eclipse shortcut only finds files from your workspace and doesn’t dig at jar level.

3) Ctrl + 1 for quick fix

This is another beautiful Eclipse shortcut which can fix up any error for you in Eclipse. Whether it’s missing declaration, missing semi-colon or any import related error this eclipse shortcut will help you to quickly sort that out.

4) Ctrl + Shift + o for organize imports

Another Eclipse keyboard shortcut for fixing missing imports. Particularly helpful if you copy some code from other file and what to import all dependencies.

Eclipse Shortcut for Quick Navigation

In this section, we will see some Eclipse keyboard shortcut which helps to quickly navigate within the file and between file while reading and writing code in Eclipse.

5) Ctrl + o for quick outline going quickly to method

6) Alt + right and Alt + left for going back and forth while editing.

7) Alt + Shift + W for show in package explorer

8) Ctrl + Shift + Up and down for navigating from member to member (variables and methods)

9) Ctrl + k and Ctrl + Shift +K for find next/previous

10) Go to a type declaration: F3, This Eclipse shortcut is very useful to see function definition very quickly.

Attaching source code of JDK in Eclipse IDE

There are multiple ways to attach a source in Eclipse for any JAR file, but I found the following approach as the most simple one because it attaches code directly to the JAR file by right-clicking on it.
1. Select any Java project2. Expand Referenced libraries3. Select any JAR file, in our case rt.jar which is Java runtime4. Right-click and go to properties5. Attach source code by browsing the source path.
After performing the above 4 steps following window will appear where you can browse the source code for any JAR file. In our case source code for rt.jar is src.zip which can be found in the JAVA_HOME directory or JDK installation directory on your computer.

How to attach source in Eclipse - step by step guide

Now let’s see What difference attaching source code in Eclipse makes. Suppose you want to see the code of HashSet class to find out How HashSet works in Java or the source code of java.lang.String class to find out the internal implementation of String class. 

Let’s use Eclipse shortcut to find any Type e.g. ctrl+T and select java.lang.String class. If you don’t have source code attached for rt.jar or JDK, as java.lang.String belongs to rt.jar, it will display java.lang.String class in class file editor as shown below.

Attaching source in Eclipse for debugging Java JDK

Once you attach source code in Eclipse for JDK by following the above steps, select java.lang.String again. This time, you will see the proper source for the String class from rt.jar as shown in the following pic.

How to attache source in Eclipse for JAR for debugging

 That’s all on How to attach source code in Eclipse for any JAR for debugging or code lookup purposes. I personally add source code of JDK and frequently used libraries like Spring, Hibernate, or Apache commons and leave the rest of JAR for the decompiler to take care of. You can attach source code for even your in-house libraries which is quite common in Investment banks.

Java Pattern Queries We provide a set of 32 build-in queries for programming idioms, design patterns and bug detection. All queries are in the SOUL template query language. The queries are not comprehensive merely a preliminary set of queries for common idiomatic occurrences in Java code. The following queries were among those implemented to detect Java code idioms: for loop, while loop, enhanced for loop, if statement, if else statement, do while loop, type narrowing, class getter, class setter, file read, file write, file open-read-file close, file open-write-file close, visit ArrayList, creation of JFrame, and creation of JApplet. The set of design pattern queries is as follows: Composite, Decorator, Factory, Iterator, Observer, Prototype, Singleton, Template and Visitor. The bug detection queries are for locating possible runtime errors: they include read/write after close, read/write after connection close (socket), null pointer invocation, no default constructor, using the wrong equals (== instead of equals), no setter, no getter, close in catch block, and close split from get stream. Note that the queries may return examples of valid source code (false positives) that the programmer had intended. Here are a few examples of queries. Details of the SOUL template syntax are available online6. This is the query to detect class setters: 6 http://soft.vub.ac.be/SOUL/home/query-syntax/ ICSSEA 2013-7 O’Hanlon, O’Riordan 4/8 if jtClassDeclaration(?class){ class ?className { private ?fieldDeclarationType ?fieldName; ?modList ?returnType ?methodName(?type ?var){ ?fieldName = ?var; } } } In this example the variable ?className will be bound to classes containing a method ?methodName and a private field ?fieldName. A match has to exhibit all properties exemplified by the code snippet, but can have additional properties. Here is the bug pattern query for invocation with a null pointer: if jtMethodDeclaration(?m,?interpretation) { ?modList ?type ?name(?pList) { if (?x == null) ?x.?message(?aList); } }, absolutelyNot(jtMethodDeclaration(?m,?interpretation) { ?modList ?type ?name(?pList) { if (?x == null) { ?x = ?exp; ?x.?message(?aList); } } }) Here is the query for the singleton design pattern: jtClassDeclaration(?singleton, ?interpretation) { class ?singletonName { static ?singleton ?uniqueInstance = new ?singleton(); ![public ?singleton(?paramList){}]; public static ?singleton::jtType ?instance() { return ?uniqueInstance; } } }

Conclusion

I’m not a great Java programmer and I know nothing about Eclipse. But I do need to know how to open a simple Java project in Eclipse. I want it explained to me in very simple terms, with clear examples.

0 Comments

No Comment.