How do I install Gradle in Mac OS X?

Gradle

In this post we will learn how to install Gradle in OS X. The following steps will guide our installation process to make Gradle available in our OS X machine. But before we start let’s take a look at the definition from wikipedia about Gradle.

Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven of declaring the project configuration.

From: Wikipedia

I. Using Homebrew

The short and simple answer is to use the Homebrew package manager for macOS. You can visit the website for detail on how to install the Homebrew. But to help you, I’ve copied the online script to install it below:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After installing Homebrew, just type the following command to install Gradle.

brew install gradle

Now, if you want to do it manually, here are the steps 😉

II. Manual Installation Steps

1. Download Gradle

To download visit Gradle Releases Page. Download the complete distribution which includes binaries, sources and offline documentation. For example, you can download the latest release of Gradle, as of this update the version is gradle-4.0.2-all.zip.

2. Unpacking Gradle and Configure Environment Variables

  • Open Terminal.app.
  • Create a new directory sudo mkdir /usr/local/gradle.
  • Extract the downloaded Gradle distribution archive by executing sudo unzip gradle-4.0.2-all.zip -d /usr/local/gradle.
  • Edit .bash_profile in your home directory to add GRADLE_HOME variable with the following line export GRADLE_HOME=/usr/local/gradle/gradle-4.0.2
  • Also update the PATH variable to include $GRADLE_HOME/bin. If you don’t already have the PATH variable add the following line export PATH=$GRADLE_HOME/bin:$PATH
  • Run source ~/.bash_profile to executes the update version of .bash_profile. Or you can open a new Terminal.app to make this changes active.

3. Running the Installation

To test Gradle installation open the Terminal.app and execute gradle -v command. If your installation was correct you will see something like the following output:

$ gradle -v

------------------------------------------------------------
Gradle 4.0.2
------------------------------------------------------------

Build time:   2017-07-26 15:04:56 UTC
Revision:     108c593aa7b43852f39045337ee84ee1d87c87fd

Groovy:       2.4.11
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_121 (Oracle Corporation 25.121-b13)
OS:           Mac OS X 10.12.6 x86_64

Installing and Running MongoDB in Windows 7

What is MongoDB

In this post you will see how to install and running MongoDB database server on Windows 7. What is MongoDB? MongoDB is a NoSQL database. MongoDB is a non-relational JSON document store, a document oriented database. Non-relational means that it does not support the relational algebra that most often expressed in SQL like what RDBMS such as Oracle or MySQL does. The document here is not a Microsoft Word documents or such, but it is a JSON (JavaScript Object Notation) documents.

And if you don’t know what it is look like, here are some examples of JSON documents:

[
  {"firstName" : "John", "lastName" : "Doe", "cityOfBirth" : "New York"},
  {"firstName" : "Julia", "lastName" : "Roberts", "movieTitles" : ["Pretty Woman", "Notting Hill", "Runaway Bride"]}
]

Other characteristics of MongoDB are: it has a dynamic schema, it does not support SQL language, it does not support Joins, and it doesn’t support transactional across multiple collections.

MongoDB Installation

After you know a bit of MongoDB, lets get started with the installation process. Here are the step by step of the MongoDB installation.

  • Download Windows 64-bit MongoDB installer at https://www.mongodb.com/download-center. When this post is written the binary file name for Windows 64-bit is mongodb-win32-x86_64-2008plus-2.6.3-signed.msi.

  • After you have finished download the installer, double-click the file to start the installation wizard.

Welcome to MongoDB Setup

Welcome to MongoDB Setup

  • Press the Next button for the next screen and check I accept the terms in the License Agreement check box and press the Next button to continue.
End-User License Agreement

End-User License Agreement

  • The next step is to choose the setup type. There are three types of setup available, Typical, Custom and Complete. For now, we will choose Complete. So click the Complete button to continue the installation process.
Choose Setup Type

Choose Setup Type

  • Press the Install button to begin installation process.
Ready To Install MongoDB

Ready To Install MongoDB

  • After pressing the Install button, you can see the screen of MongoDB installation process. Wait until the installation is done.
Installing MongoDB

Installing MongoDB

  • And finally you have the MongoDB database installed. Click the Finish button to end the installation process.
Completed MongoDB Installation

Completed MongoDB Installation

The steps above have finalized your MongoDB installation. If you check in your C:\Program Files directory you will see the MongoDB installation directory in there. There will be a bin directory under C:\Program Files\MongoDB 2.6 Standard where all the MongoDB application files. Now you have installed the database server lets run and check the database server.

Running MongoDB

For this step we will focus on two files from the bin directory of the MongoDB installation. The mongod.exe and mongo.exe. The first executable is the MongoDB database engine daemon while the second executable is the shell program to access the MongoDB.

To run the database do the following steps:

  • Create data directory. By default, MongoDB looks for \data\db directory in the root Drive from where you run the mongod. For example, you can create C:\data\db. Or you can use the --dbpath argument to tell MongoDB where to store the data.
  • Open Command Prompt and cd to C:\Program Files\MongoDB 2.6 Standard\bin and type mongod to start the daemon.
Running MongoDB mongod

Running MongoDB mongod

The screen above shows you that the MongoDB is successfully started, using the dbpath \data\db and it is ready and listening for connections on the default port 27017.

Running The Shell

  • Open Command Prompt and cd to C:\Program Files\MongoDB 2.6 Standard\bin.
  • Run mongo.exe to start the shell. You’ll see a welcome message to the MongoDB shell.
Running MongoDB Shell

Running MongoDB Shell

  • In the shell above we run a couple of commands:
  • use peopledb command ask the MongoDB to change to the persons collections, if it doesn’t exist Mongo will create one.
  • To add document to the collections we can call db.persons.insert(); and passing the JSON document as the arguments.
  • To query the collection we can use db.persons.find().
  • If you want for instance to find Julia in the collection you can do db.persons.find({"firstName" : "Julia"})
  • To close the shell we can call quit() command.

That’s all for now, I hope this post is useful for you. In the next post I will show you how to create a simple Java application that use Mongo Java Driver to store data using Java programming into the MongoDB database. So, see you in the next post. Thank you.

Java SE Installation Tutorial

Summary: This tutorial is about Java Development Kit (JDK) Standard Edition installation guide. After reading and executing this tutorial you will be able to install the JDK and setting the basic configuration for running the Java compiler from the command line. You will also learn how to create, compile and run a simple Hello World program written in Java.

Download The JDK

When this tutorial is written, the latest JDK is version 7 Update 45. To download the Java SE Development Kit 7 (JDK 7) click to following URL: Java SE Downloads. Make sure you choose the Windows version and download the JDK and not the JRE. The JDK installer will have the JRE included.

Installing The JDK

After downloading you will have a file called jdk-7u45-windows-i586.exe. Executing this file, either by double-click the file or running it from the command prompt, will begin the installation process. You will see the following screens during the installation process:

Installation Wizard Welcome Screen

Installation Wizard Welcome Screen

To continue the installation click the Next button shown in the screen above. This action will bring you to the feature’s selection screen in the installation wizard shown below.

Features Selection

Features Selection

In the feature’s selection screen above you will be able to choose which features to install. For the simplicity of this tutorial we will choose to install the entire features. We will also use the default installation path. In Windows operating system the default path is C:\Program Files (x86)\Java\jdk1.7.0_45. Click the Next button to continue to the next step.

Installation Progress

Installation Progress

The screen above shows the progress status of the JDK installation. Depending on your computer speed this process may take a couple of minutes to finish. Wait for the next screen to show up.

JRE Installation Screen

JRE Installation Screen

As stated above that the JRE is included within the JDK installer, the screen above show you the JRE installation wizard. We will use the default path as shown above C:\Program Files (x86)\Java\jre7. Click the Next button to continue to the next step.

JRE Installation Progress

JRE Installation Progress

The screen above shows the status of the JRE installation. Depending on your computer speed this process may take some other minutes to complete. When the JRE installation complete you will see the next screen.

JDK Installation Completed

JDK Installation Completed

Seeing the screen above means that your JDK installation is completed. Click the Finish button to exit the installation Wizard.

Configuring The JDK

Now you have the JDK installed on your system. Configuring the JDK so that you can run the javac and java commands from the command prompt is another important steps. A lot of beginners faced with a problem that the javac command is not recognized because they did not configure the path correctly. To validate your installation type the javac command in the command prompt. At first, you will see the following screen as the command output.

javac command is not recognized

javac command is not recognized

To make the javac command run properly you need to update the PATH environment variable to include the Java binary installation path. The following steps will show you how to configure the PATH environment variable.

  • Open your Windows Explorer, you can use the Windows + E shortcut.
  • Right click on Computer icon and choose the Properties menu.
  • Click Advanced system setting from the Task list.
  • In the System Properties window choose the Advanced tab.
  • Click the Environment Variables button.
  • In the Environment Variables windows you will see two sections. User variables and System variables.
  • Find the Path variable and click the Edit button.
  • In the Edit User Variable screen, update the current variable value by adding the path to JDK bin folder, in our case the path will be C:\Program Files (x86)\Java\jdk1.7.0_45\bin;. You should add this information at the beginning at the beginning to make sure it will be picked first.
  • Press the OK button to update the User Variable.
  • Press the OK button to finish the Environment Variables setting.
  • Press the OK button on the System Properties window to close it.

Now you can open a new command prompt window and type the javac command. You will see that the javac command is now run as expected. You can type javac -version to check the version of your JDK.

Java Command Recognized

Java Command Recognized

Creating The HelloWorld.java

Now you have the JDK installed and configured properly. Let’s now create our first Java program, the famous Hello World program. Let’s begin.

  • Open a command prompt window.
  • Create a directory C:\HelloWorld\src.
  • Go to the src directory and create another directory call program.
  • Go to the program directory.
  • Create the HelloWorld.java source file by typing notepad.exe HelloWorld.java.
  • The Notepad program will show a dialog saying that the HelloWorld.java file is not found.
  • Press the Yes button for creating the file.
  • Type the following code in the Notepad editor.
package program;

public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello World");
   }
}
  • Click the Save menu to save your program.
  • Click the Exit menu to exit from the Notepad program.
  • Type the dir command, and you will see that you have created a file called HelloWorld.java.

Compiling Java Source File

Up to this step you have created your first Java program. Before running the program you need to compile it. The compilation process compiles your Java source file into something called the Java bytecode. The bytecode is an intermediate language that makes the Java program portable across different platform.

To compile the program do the following step:

  • Go to the C:\HelloWorld\src\program directory.
  • Type javac HelloWorld.java to compile the source code.
  • A successful compilation will produce a HelloWorld.class file. This class file is your binary HelloWorld program, and it is ready to be executed.
Compiling Hello World

Compiling Hello World

Running the Program

Now you are in the final step of the tutorial. Running the HelloWorld program. Here are the final step you need to run:

  • Go to the C:\HelloWorld\src directory.
  • Type java program.HelloWorld without the .class extension to run the program.
  • You will see that the Hello World string printed on the screen.
Running Hello World

Running Hello World

That’s all for now, see you on another tutorial.

Introduction to Apache Maven

In this post you will learn about Apache Maven. What is Maven? In simple words, Maven is a tool that we can use to build and manage a Java based project. Compare to the older type of build tool such as Ant, which is also an Apache project, Maven gives developers a standard way to build projects, a clear definition of the projects consisted of, an easy way to publish project information and an easy management of the project libraries dependency.

In Maven, the project libraries (Jars) will be maintained in the Maven repository and can be shared among projects. We don’t need to include the Jars in the project’s source code, as what we usually do when using tool like Ant. And here are the main objectives of the Apache Maven project:

  • Making the build process easy.
  • Providing a uniform build system.
  • Providing quality project information.
  • Providing guidelines for best practices development.
  • Allowing transparent migration to a new features.

After you know a little about Maven, let’s continue to the next step, installing the Apache Maven.

Step 1. Downloading Apache Maven

  • Go to Apache Maven and from the Get Maven section on the left sidebar click Download link.
  • Download the appropriate installer for your platform. For example, I am downloading the .zip file. The latest version when this post is written is Maven 3.1.0 (apache-maven-3.1.0-bin.zip).
  • Extract the downloaded zip file. For example, I am extracting it into D:\Toolbox\apache-maven-3.1.0 directory.

Step 2. Configuring Environment Variables

  • After extracting Maven distribution you have to define M2_HOME environment variable. The value of this variable is the path to your Maven installation.
  • You can create environment variable using the following steps:
    • Right click on My Computer.
    • Select Properties menu.
    • Select Advanced system settings.
    • In the System Properties window select the Advanced tab and click the Environment Variables button.
    • Add the variable in the System variables section.
    • Click OK to proceed.
  • Add the %M2_HOME%\bin; into your PATH environment variable so that you can execute Maven command from any path inside your command prompt. This PATH variable can also be updated in the System variables section.

  • Open your command prompt and type echo %M2_HOME%. Pressing Enter and you should see the value of the variable printed on the console. For example:
D:\>echo %M2_HOME%
D:\Toolbox\apache-maven-3.1.0
  • To check if Maven also in your Path variable you can execute echo %PATH% in your command prompt. You should see the path to Maven binary in the Path variable.
D:\>echo %PATH%
D:\Toolbox\apache-maven-3.1.0\bin;C:\Program Files (x86)\Java\jdk1.7.0_13\bin;

Step 3. Running Maven

  • Now you have Maven installed and configured. Let’s run Maven for the first time.
  • To run Maven we use the mvn command.
  • For example to check the version of the Maven we run mvn -version
  • You will see the following output, more or less, it depends on the JDK version you have in your machine.
D:\>mvn -version
Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 10:15:32+0800)
Maven home: D:\Toolbox\apache-maven-3.1.0
Java version: 1.7.0_13, vendor: Oracle Corporation
Java home: C:Program Files (x86)\Java\jdk1.7.0_13\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

Congratulations! You’ve installed Apache Maven successfully on your development machine. On the next post I will show you how to create a new project using Maven.

Introduction to JetBrains IntelliJ IDEA Community Edition

In this post I will introduce to you the JetBrains IntelliJ IDEA, one the most powerful IDE (Integrated Development Environment) for Java development or other languages that run on the JVM such as Groovy and Scala. IntelliJ IDEA comes in two editions, the IntelliJJ IDEA Ultimate Edition and the IntelliJ IDEA Community Edition.

The IntelliJ IDEA Community Edition is an open source version of Intelli IDEA. That’s mean you can use it for free. If you work with standard Java, Groovy, Scala or mobile development using Android then the Community Edition can be your choice. If you work with in web or Java EE development then the Ultimate Edition is the way to go.

Here are some features that you will get when using the IntelliJ IDEA Community Edition:

  • An Intelligent code editor that has all the smarts for understanding Java, XML and Groovy code.
  • Refactorings, code inspections and intentions, super-fast navigation and search
  • Testing framework integration: JUnit and TestNG
  • Build tools support: Ant and Maven
  • Popular version control systems integration: CVS, Subversion and Git
  • Swing UI designer

After a bit of introduction, let’s begin the installation.

Step 1. Getting the Installation Distribution

  • Download the installation package of the IntelliJ IDEA Community Edition.
  • Click Download IntelliJ IDEA Community Edition to go the download page.
  • On the page you can see two editions, select the Community Edition. The current version available when this post was written is the version 12.1.4. You can also see that the download are available for Windows, Mac OS X and Linux. For this post I will download the Windows version. The download size for the Windows installer is around 130 MB.

Step 2. Install IntelliJ IDEA Community Edition

  • To begin, double-click the installer you’ve downloaded, for example ideaIC-12.1.4.exe.
  • You’ll be greeted with the following screen
intellij-idea-community-edition-setup-step-1

IntelliJ IDEA Setup Welcome Screen

  • Press the Next button to continue.
  • You’ll be asked the installation location. On Windows it will be defaulted to the C:\Program Files directory.
  • Press Next for a couple of times until the installation complete.
  • When you choose to run the IntelliJ IDEA for the first time you’ll get the following screen
Installation Complete Screen

Installation Complete Screen

  • Press OK button to start IntelliJ IDEA for the first time. You will be shown a splash screen right after you press the button and continued by the screen welcoming you to the IDE.
Welcome to IntelliJ IDEA

Welcome to IntelliJ IDEA

Step 3. Now you have the IntelliJ IDEA Community Edition installed in your machine. Let’s create your first program using IDEA.

  • From the Welcome Screen above click the Create New Project menu.
  • A New Project dialog window will be shown. In this dialog you can do:
Create a New Project Window Dialog

Create a New Project Window Dialog

  • Enter the Project Type, choose Java Module.
  • Define the Project Name. We’ll just name it HelloWorld.
  • Choose the Project Location directory.
  • Select the JDK version you’ll be using.
  • Press the Next button followed by the Finish button to continue.

Step 4. IntelliJ IDEA Main Window, creating HelloWorld.java

  • Here is the Main Window of IntelliJ IDEA.
IntelliJ IDEA Community Edition

IntelliJ IDEA Community Edition

  • The bigger screenshot of the project explorer panel.
Project Explorer Window

Project Explorer Window

  • Before we create a class let’s create a package.
  • Right-click the src folder, continued by selecting the New and Package menu.
  • Enter the package name, for instance org.kodejava.introduction
  • To create a class right click on the newly created package, select New and Java Class menu.
  • In the Create New Class dialog enter HelloWorld in the Name text field and choose Class from the Kind dropdown selection.
  • Type the following code in the editor.
package org.kodejava.introduction;

public class HelloWorld {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        HelloWorld hello = new HelloWorld();

        String name = "world";
        hello.setName(name);
        hello.sayHello();
    }

    private void sayHello() {
        System.out.println("Hello " + getName() + "!");
    }
}
  • Press Shift + F10 or click the green Run button to execute your code.
  • And we are done!

Other things you can do next is to learn more about the menu and the keyboard shortcuts that you can use while working with the IDE. One good place is to look at the Help, Tip of the Day menu.

That’s finishes our introduction to the IntelliJ IDEA Community Edition IDE. I hope you can continue your learning and this IDE will surely help you to understand Java better. Because it is not just a tool to write code, it will give you a lot of suggestion so that you can always improve your code quality.