Java and Maven

Run your Java console application in Eclipse

  1. Update the content of HelloWorld class to display a message to console.
public class HelloWorld {

	public static void main(String[] args) {
		// display Hello World to console
		System.out.println("Hello World");
	}

}
  1. To run the program, click on the arrow next to the green play button > Run As > Java Application.
  1. Message “Hello World” will be displayed in the console.

Debug your Java console application in Eclipse

  1. To debug your program, first you need to set a breakpoint.
  2. To set a breakpoint, right-click on the line number, then select Toggle Breakpoint.
  1. To start the debugger, click on the Debug button, and select the project name.
  1. Click “Switch” on the Confirm Perspective Switch screen.
  1. The program will stop at the breakpoint and wait for your next step.
  1. On the debugger tool menu, select Step Into (F5) to continue.
  1. The message “Hello World” will be displayed in the console. Press F5 once again to end the program.

Introduction to Maven Dependency and Maven Central Repository

Reference: https://maven.apache.org/guides/introduction/introduction-to-repositories.html

A repository in Maven holds build artifacts and dependencies of varying types. There are exactly two types of repositories: local and remote:

  • The local repository is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.
  • The remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and https://. These repositories might be a truly remote repository set up by a third party to provide their artifacts for downloading (for example, repo.maven.apache.org). Other “remote” repositories may be internal repositories set up on a file or HTTP server within your company, used to share private artifacts between development teams and for releases.

Local and remote repositories are structured the same way so that scripts can run on either side, or they can be synced for offline use. The layout of the repositories is completely transparent to the Maven user, however.

Maven Central Repository can be found here This is where you can find a list of all available dependencies that can be added to your project.

Using Repositories

In general, you should not need to do anything with the local repository on a regular basis, except clean it out if you are short on disk space (or erase it completely if you are willing to download everything again).

For the remote repositories, they are used for both downloading and uploading (if you have the permission to do so).

Add a Dependency to Your Java Project

Reference: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

For a list of dependencies please go here https://mvnrepository.com/

Let’s try to add a dependency to our project. First, find the dependency from the maven repository.

Click on the “Apache Log4j Core”, it will be redirected to the details page. Select the current/stable version.

The copy the codes under Maven tab to your pom.xml file.

Java standard naming convention

All Java components require names. Names used for classes, variables and methods are called identifiers. In Java, there are several points to remember about identifiers. They are as follows:

  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • A keyword cannot be used as an identifier.
  • Most importantly, identifiers are case sensitive.
  • Examples of legal identifiers: age, $salary, _value, __1_value.
  • Examples of illegal identifiers: 123abc, -salary.
  • A class name should start from a capital case letter and long names should use camel casing. For example: TaxationDepartment
  • Object name should start from lower case letter and long names should use camel casing. For example: taxationDepartment