Sunday, May 28, 2017

Eclipse Tips

Popular Eclipse nice-to-have plugins:

CallGraph -- visually see who are the callers and callees of a method
ControlFlowGraph Generator -- see the control flow of a block of code
JRebel  -- enhance debugging feature, update changes live without rebuild
TestNG -- unit test
Darkest Dark -- editor theme
SpotBugs
CheckStyle
M2Eclipse
Unnecessary Code Detector
Bytecode outline
PyDev -- python editor

How to enable Visual Class in Eclipse?


Figure 1A


The ability to create new visual class does not come automatically with Eclipse. If you cannot create new visual class in Eclipse, you need to install the Window Builder and Swing Designer packages. These can be installed via the Help->Install New Software menu item.

Figure 1B

Figure 1C

Once you have installed these packages, there should be a toolbar button to create the visual classes as shown in Figure 1A. The visual class menu item also will also show up under the File->New->WindowBuilder->Swing Designer... menu.

How to install the color theme plugin?

Goto help->install new software
Click the add button to add a new repository and put this URL in the location text box:
http://eclipse-color-theme.github.io/update/
Figure 1D

Once you have put in the URL and click ok, it should show Eclipse Color Theme as an item that you can select to install. Install the Color Theme plugin.
Once you are done, you can go to Preferences->General->Appearance->Color Theme to pick the color theme you want.



Eclipse shortcuts cheat sheet:

Shortcut Key MacShortcut Key WindowsDescription
Command + 3Ctrl + 3In Eclipse Juno, puts the focus into Quick Access search box, in older eclipse opens quick access dialog.
Command + SCtrl + SSave current editor
Command + 1Ctrl + 1Quickfix for errors and warnings, depends on the cursor position
Control + SpaceCtrl + SpaceContent assist and code completion
Command + Shift + FCtrl + Shift + FFormat source code
Control + QCtrl + QMoves cursor to the last edited position
Command + DCtrl + DDeletes current line in the editor
Command + Shift + OCtrl + Shift + OOrganize imports in the current java file
Command + 2 + LAtl + Ctrl + LAssign statement to new local variable
Command + 2 + FCtrl + 2 + FAssign statement to a field
Command + OCtrl + OShows quick outline of the java class
Command + fn + F11Ctrl + F11Runs the current opened java class if main method exists or else run the last launched application
Command + Shift + RCtrl + Shift + ROpen / Search for resources, quickly search for a file
Command + Shift + TCtrl + Shift + TOpen / Search for types, very useful in finding classes
Command + ECtrl + ETo select an editor from the currently open editors
Command + fn + F8Ctrl + F8Shortcut for switching perspectives
Command + [ or Command + ]
Using Mac keyboard on Linux VM
Command + Alt + left or
Command + Alt + right
Alt + ← or Alt + →

Go to previous/ next editor position in history
Fn + F3F3Move cursor to the declaration of the variable
Command + Shift + PCtrl + Shift + PMove cursor to the matching bracket, the cursor has be after the opening bracket
Command + .Ctrl + .Go to the next problem
Command + Shift + .Ctrl + ,Go to the previous problem
Fn + F4F4Show type hierarchy of the variable
Command + KCtrl + KFind next for search text in the opened editor
Command + Shift + GCtrl + Shift + GSearch for references in the workspace
Command + TCtrl + TShows type hierarchy of the current java class
Command + MCtrl + MMaximize Java editor
Fn + Shift + F2Shift + F2Shows the javadoc of the method, class
Command + Option + RAlt + Shift + RRename of package, class etc
Command + Option + TAlt + Shift + TOpens the quick refactoring menu
 http://crunchify.com/my-favorite-eclipse-shortcuts-for-mac-and-windows/

Command + left mouse click on a method name or class name   -- Go the the method or class definition. Notice when you hold down the command button, the class or method name will be underlined whenever you move the mouse over it.

How to import an ant build.xml into Eclipse?



Figure 2A


Figure 2B

When you finish with the new project dialog, Eclipse will pull in everything needed to create a project from the build.xml file.


How to get rid of unresolved symbols in Eclipse?

 When this error occurs, Eclipse does not complain about the java class where this symbol is defined.
This mean Eclipse can locate the Java class. One of the issues here is you can have multiple jar files defining the package the class belong to and some of them are old and do not define the symbols Eclipse is complaining about. You can have multiple Jar files defining the class. The best to tell what Jar files contain the class is to launch the open type dialog  (ctrl+shift+t) and type in the  package and class name. This will list all the Jar files defining the class. If you have multiple Jars file, make sure you include the latest Jar file first in the project include libraries settings. If possible, remove and include only one Jar including the latest version of class  where the unresolved symbols can be found.

How do you tell Eclipse to run just one ant target?


The build.xml file can have many ant targets, and the project can contain many packages which may not be relevant to what you are doing. If you want to run just one particular Ant target, you can follow this step.
First you need to open the navigator view, and from there select and right click on the build.xml file.
When the right click menu pops up, select the Ant Build... menu item to shows the configuration dialog.

Figure 3A


From the configuration dialog, you can select what Ant target to run.


Figure 3B



Eclipse fails to start

Once in a while when you start Eclipse, you may see this startup error:

Figure 4A

First, check what version of Java is running on your machine.
Does it match the version Eclipse is expecting? Make sure both your Eclipse and Java version are 32-bit or 64-bit. You cannot have them both mixed.
To check if your Java version is 64-bit or not run:
>java -d64 -version
If it says anything about 64-bit is not supported, then you have a 32-bit version.

Second, make sure the -vm path set in your eclipse.ini file point to a Java version compatible with your Eclipse. This error usually shows up after your java version gets updated by some program.
Consequently, the new updated Java version is not compatible with your old Eclipse version.
To make your Eclipse running, you can change the path to the -vm option to point to the previous Java SDK
version compatible with your Eclipse.
Here the path is changed to point to jdk1.7.0:

-vm
C:\Program Files\Java\jdk1.7.0_04\bin\javaw.exe
--C:\\ProgramData\\Oracle\\Java\\javapath\\javaw.exe
--launcher.append
-Vmargs
-Xms256m
-Xmx1024m

Lines starting with "--" are commented lines.


How to attach Eclipse to an ANT target?


ANT_OPTS=-Xms256M -Xmx2048M -XX:MaxPermSize=128M

set ANT_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005  -Xms256M -Xmx2048M -XX:MaxPermSize=128M

When you run a Java program with the above options or arguments, it will allow Eclipse to connect to port 5005 of the JVM to debug the program. Notice this is very handy if you have a debug configuration setup for the Java program yet. Just run the Java program as normal and tell Eclipse to debug remotely to port 5005.

Debugging JNI applications

In order to debug both C++ and Java in Eclipse, you need to make sure your debugged settings are correct. In the screenshots below, I am trying to create a debug configuration to debug the C++ code called by a Java program.




Here I am trying to create a C/C++ Attach to Application debug configuration. When we run this debug configuration, Eclipse will prompt us about what process to attach. In this case, we will attach to a Java program which will call the C/C++ code or libraries.


Here GDB is used as the debugger for the C/C++ code.


Even after we setup everything above and able to run GDB, we may see this error where GDB cannot locate the C/C++ source code during the debugging. If it cannot locate the source code, then Eclipse won't be able to show the source code to you, and you won't be able to step through the code and debug anything. This implies that the source path is not setting up correctly in Eclipse.


To fix this error, we have to explicitly add the path for the compilation directory. This is very important. Notice the other options/lookup paths (Absolute File Path, File System Directory, Path Mapping and etc) shown in the screenshot won't work.


Eclipse error “Could not find or load main class”

Try to point the classpathentry output path in the .classpath file to target/classes The classpath file is located in the project folder. Edit it manually.
In my case, it was like this before:
classpathentry kind="output" path="bin"
I changed it to:
classpathentry kind="output" path="target/classes"