Thursday, September 28, 2017

Setting JDK in Eclipse

Window -> Preferences -> Java -> Installed JRE's tab.
  • If you see JRE you want in the list select it (selecting a JDK is OK too)
  • If not, click Search…, navigate to Computer > Windows C: > Program Files > Java, then click OK
  • Now you should see all installed JREs, select the one you want
  • Click OK/Finish a million times

FAQ How do I increase the heap size available to Eclipse?

Some JVMs put restrictions on the total amount of memory available on the heap. If you are getting OutOfMemoryErrors while running Eclipse, the VM can be told to let the heap grow to a larger amount by passing the -vmargs command to the Eclipse launcher. For example, the following command would run Eclipse with a heap size of 256MB:
eclipse [normal arguments] -vmargs -Xmx256M [more VM args]
The arguments after -vmargs are directly passed to the VM. Run java -X for the list of options your VM accepts. Options starting with -X are implementation-specific and may not be applicable to all VMs.
You can also put the extra options in eclipse.ini.
Here is an example;
-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.200.v20120913-144807
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Xms512m
-Xmx1024m
-XX:+UseParallelGC
-XX:PermSize=256M
-XX:MaxPermSize=512M 
 
 
 
Meanings
-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xss<size>        set java thread stack size 

Using Maven within the Eclipse IDE - Tutorial

This tutorial describes the usage of Maven within the Eclipse IDE for building Java applications.

1. Using Maven with the Eclipse IDE

The Eclipse IDE provides excellent support for the Maven. This tooling is developed in the M2Eclipse project.
This tooling manages the project dependencies and updates the classpath of the project dependencies in the Eclipse IDE. It ensures that the Maven experience in Eclipse is as smooth as possible. The tooling also provides different kind of wizards import andto create new Maven based projects.
It also provides an editor for the pom.xml Maven configuration file via a structured interface. You can select the tab labeled pom.xml to edit the XML data directly.
M2E pom editor
M2E pom editor

2. Installation and configuration of Maven for Eclipse

2.1. Install the Maven support for Eclipse (m2e)

Most Eclipse downloads include the Maven tooling already. If it is missing in your installation, you can install it via the main update of your release via Help ▸ Install New Software. The following listing contains the update site for the Neon release and an update site maintained by the m2e project.
// Neon update site
http://download.eclipse.org/releases/neon

// Update site provided by m2e project
http://download.eclipse.org/technology/m2e/releases
For the usage of Maven for Java projects, you only need the m2e component. For Java web development you also want the m2e-wtp entry.
m2e installation

2.2. Download the Maven index

By default, the Maven tooling does not download the Maven index for the Eclipse IDE. Via the Maven index you can search for dependencies, select them and add them to your pom file. To download the index, select Windows ▸ Preferences ▸ Maven and enable the Download repository index updates on startup option.
Download the Maven index
After changing this setting, restart Eclipse. This triggers the download of the Maven index. You may want to remove this flag after restarting to avoid network traffic at every start of Eclipse.
The m2e team works on a way to dynamically query for dependencies. Please register for the following bug to show that you are interested in this development: Provide an alternative Artifact search mechanism in Eclipse Maven

3. Exercise: Create a new Maven enabled project via Eclipse

This exercise demonstrates the creation of a new Maven enabled project in Eclipse.

3.1. Create Maven project

Create a new Maven project via File ▸ New ▸ Other…​ ▸ Maven ▸ Maven Project.
Create Maven project in Eclipse - Part 1
On the first wizard page you can select if you want to create a simple project. In this case you skip the archetype selection. In this exercise we want to use an archetype as template for our project creation.
Create Maven project in Eclipse - Part 2
Press next, filter for the "quickstart" archetype and select the maven-archetype-quickstart entry. This is the classical Maven example archetype for project creation.
Create Maven project in Eclipse - Part 3
On the last tab enter the GAV of your project similar to the following screenshot.
Create Maven project in Eclipse - Part 4

3.2. Run the build

Validate that the generate setup works correctly by running the build. For this right-click the pom.xml file and select Run As ▸ Maven build.
Run Maven project in Eclipse
This opens a dialog which allows to define the parameters for the start. Enter clean verify in the Goals: field and press the Run button.
Run Maven project in Eclipse

3.3. Adding dependencies to your project

The Eclipse Maven tooling makes adding dependencies to the classpath of your project simple. In can directly add it to your pom file, or use the Dependencies tab of the pom editor.
Switch to the Dependencies tab and press the Add button.
me2 adddependency20
In this example we add Gson as dependency. For this we use the GAV which we found via the http://search.maven.org website. If the Maven index was downloaded (See [maven_eclipseinstallation_index] you can also search directly this dependency via the dialog.
me2 adddependency30

3.4. Use library

Change or create the App.java class in your src/main/java folder. This classes uses Gson. As Maven added it to your classpath, it should compile and you should be able to start the class via Eclipse.
package com.vogella.maven.lars;

import com.google.gson.Gson;

public class App
{
    public static void main( String[] args )
    {
        Gson gson = new Gson();
        System.out.println(gson.toJson("Hello World!") );
    }
}

4. Exercise: Add Maven support to a Java project in Eclipse

This exercise demonstrates how to convert a Java project to a Maven project.

4.1. Create Java project

Create a new Java project called com.vogella.build.maven.simple in Eclipse.
Add one class called Main. This class should have a main method, which write "Hello Maven!" to the command line.
package com.vogella.build.maven.simple;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello Maven!");
    }
}

4.2. Convert to Maven project

Select your project, right-click on it and select Configure ▸ Convert to Maven project…​.
Convert Java project to Maven
This creates a pom.xml file similar to the following.
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.vogella.build.maven.simple</groupId>
  <artifactId>com.vogella.build.maven.simple</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

4.3. Execute the Maven build

Right-click the pom.xml file and select Run As ▸ Maven build.
Run Maven build from Eclipse
Enter "clean install" as Goal.
You have to enter the goals manually. The Select…​ button does not work, the dialog it displays is always empty.
Press the Finish button. This starts the build, which you can follow in the Console view.
Once the build finishes, press F5 on the project to refresh it. You see a target folder, which contains the build artifacts, e.g., a JAR file.

5. Exercise: Create a Java web project in Eclipse using Maven

This exercise demonstrates how to create a web application in Eclipse which uses Maven. It assumes that you have already configured Eclipse for the creation of web applications.

5.1. Create Maven web project project

Create a new Maven project called com.vogella.javaweb.maven.first via the File ▸ New ▸ Other ▸ Maven ▸ Maven Project entry. On the archetype selection, select the maven-archetype-webapp entry and click the Next button.
Webproject archetype with Maven
Enter the group, artifact and version of your new Maven component.
Webproject archetype with Maven
You may see the error: The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path. To fix this, right click on your project and select Properties. On the Targeted Runtimes select your web server entry, e.g., Tomcat.

5.2. Build your project

Similar to [example_eclipsemavenproject_runningthebuild] run your mvn clean verify build command from Eclipse. Validate that there are no issues with the build.

5.3. Run on the server

Right-click your project and select the Run As ▸ Run on Server menu entry.
Start Maven project on server
Start Maven project on server
Start Maven project on server
Start Maven project on server
If you open a browser you should be able to access your webapplication.
Start Maven project on server

Eclipse Shortcuts - Tutorial

Eclipse Shortcuts. This article lists helpful Eclipse shortcuts.

1. Shortcuts

1.1. Using shortcuts in Eclipse

Using shortcuts make a developer more productive. Eclipse provides keyboard shortcuts for the most common actions. Using shortcuts is usually preferable as you can perform actions much faster.
Eclipse supports of course the typical shortcuts, e.g. Ctrl+S for saving, Ctrl+C for copying the selected text or file and Ctrl+V for pasting the element currently in the clipboard.

1.2. Shortcuts on Mac OS

This description uses the shortcuts based on Windows and Linux. Mac OS uses the Cmd key frequently instead of the Ctrl key.

2. Quick Access

The Ctrl+3 shortcut allows you to perform all available actions in Eclipse. This shortcut puts the focus into the Quick Access (quick access) search box which allows you to execute any Eclipse command. For example you can open a Preference, a Wizard, a view and a Preference page.
You can also use QuickAccess to search for an opened editor by typing in the name of the resource which the editor shows.
The following screenshot shows how you the available commands in quick access for the "New Java" search term.
Ctrl+3 shortcut dialog
Table 1. Navigation
Shortcut Description
Ctrl+Shift+R
Search dialog for resources, e.g., text files
Ctrl+Shift+T
Search dialog for Java Types
Ctrl+F8
Shortcut for switching perspectives
Table 2. Navigation between editors
Shortcut Description
Ctrl+E
Search dialog to select an editor from the currently open editors
Alt+
Go to previous opened editor. Cursor is placed where it was before you opened the next editor
Alt+
Similar Alt + ← but opens the next editor
Ctrl+Q
Go to editor and the position in this editor where the last edit was done
Ctrl+PageUp
Switch to previous opened editor
Ctrl+PageDown
Switch to next opened editor
Table 3. Navigation between views
Shortcut Description
Ctrl+F7
Shortcut for switching views. Choose the view to switch to with your mouse or cycle through the entries with repeating the keystroke
Shift+Alt+Q
Open menu for switch view keybindings
Shift+Alt+Q+P
Show package explorer
Shift+Alt+Q+C
Show console

4. Start Java programs

Table 4. Running programs
Shortcut Description
Ctrl+F11
Run last launched
F11
Run last launched in debug mode
Ctrl+Alt+B
Skip all breakpoints. Let’s you use debug mode for code reloading
Alt+Shift+X+J
Run current selected class as Java application
Alt+Shift+X+T
Run JUnit test
Alt+Shift+X+P
Run JUnit Plug-in test

5. Editing in the Java editor

Table 5. Handling the editor
Shortcut Description
Shift+Alt+
Selects enclosing elements.,result depending on cursor position
Table 6. Handling the editor
Shortcut Description
Ctrl+1
Quickfix; result depending on cursor position
Ctrl+Space
Content assist/ code completion
Ctrl+T
Show the inheritance tree of the current Java class or method.
Ctrl+O
Show all methods of the current class, press Ctrl + O again to show the inherited methods.
Ctrl+M
Maximize active editor or view
Ctrl+Shift+F
Format source code
Ctrl+I
Correct indentation, e.g., format tabs/whitespaces in code
Ctrl+F
Opens the find dialog
Shift+Enter
Adds a link break at the end of the line
Ctrl+Shift+O
Organize the imports; adds missing import statements and removes unused ones
Alt+Shift+Z
Wrap the select block of code into a block, e.g. try/catch.
Table 7. Cursor navigation and text selection
Shortcut Description
Ctrl+ or Ctrl+
Move one text element in the editor to the left or right
Ctrl+ or Ctrl+
Scroll up / down a line in the editor
Ctrl+Shift+P
Go to the matching bracket
Shift+Cursor movement
Select text from the starting position of the cursor
Alt+Shift ↑ / ↓
Select the previous / next syntactical element
Alt+Shift ↑ / ↓ / ← / →
Extending / reducing the selection of the previous / next syntactical element
Table 8. Copy and move lines
Shortcut Description
Ctrl+Alt+
Copy current line below the line in which the cursor is placed
Ctrl+Alt+
Copy current line above the line in which the cursor is placed
Alt+Up
Move line one line up
Alt+Down
Move line one line down
Table 9. Delete
Shortcut Description
Ctrl+D
Deletes line
Ctrl+Shift+DEL
Delete until end of line
Ctrl+DEL
Delete next element
Ctrl+BACKSPACE
Delete previous element
Table 10. Create new lines
Shortcut Description
Shift+Enter
Adds a blank line below the current line and moves the cursor to the new line. The difference between a regular enter is that the currently line is unchanged, independently of the position of the cursor.
Ctrl+Shift+Enter
Same as Shift + Enter but above
Table 11. Variable assignment
Shortcut Description
Ctrl+2+L
Assign statement to new local variable
Ctrl+2+F
Assign statement to new field

6. Coding

Table 12. Coding
Shortcut Description
Shift+F2
Show the Javadoc for the selected type / class / method
Alt+Shift+N
Shortcut for the menu to create new objects
Alt+Shift+Z
Surround block with try and catch

7. Refactoring

Table 13. Refactoring
Shortcut Description
Alt+Shift+R
Rename
Ctrl+2+R
Rename locally (in file), faster than Alt + Shift + R
Alt+Shift+T
Opens the context-sensitive refactoring menu, e.g., displays

8. Minimum

The following shortcuts are the absolute minimum a developer should be familiar with to work efficient in Eclipse.
Table 14. Must known shortcuts
Shortcut Description
Ctrl+S
Saves current editor
Ctrl+1
Quickfix; shows potential fixes for warnings, errors or shows possible actions
Ctrl+Space
Content assist/ code completion
Ctrl+Q
Goes to the last edited position
Ctrl+D
Deletes current line in the editor
Ctrl+Shift+O
Adjusts the imports statements in the current Java source file
Ctrl+2+L or F
Assign statement to new local variable or field
Ctrl+Shift+T
Open Type Dialog
Ctrl+O
Shows quick outline of a class
Ctrl+F11
Run last launched application
Shift+F10
Opens context menu. Keyboard equivalent to Mouse2
Ctrl+F10
Opens view menu for current view.

How to auto-format code in Eclipse?

On Windows : Ctrl + Shift + F
On Mac : + + F
(Alternatively you can press Format in Main Menu > Source)