♥ 1. Setting up Java, Maven, and eclipse step by step

This is mainly for the beginners of Java to get familiarized with Java, Maven, and eclipse. This will also be handy for starting on your self-taught Java projects.

Install and configure Java

Step 1:  Download and install Java 6 or later version if available by googling “Java downloads”. Make sure that you download and install the right version for the operating system on which you will be running — for example Windows (32 bit or 64 bit), Linux, Solaris, MAC, etc. You will need both the JDK and the JRE.

Screen shot 2014-10-19 at 4.39.59 PM

The javac is the compiler that converts a source file (e.g. HelloWorld.java) to a byte code file  (e.g. HelloWorld.class). The java is the run-time command to execute a program (i.e. java HelloWorld). The src.zip is where all the library files are located and rt.jar is where the run-time class files are located.

Step 2: Set the environment variables. In a command prompt, if you type “set” (in WIN 32)  and “env” (in Unix), it will show all the environment variables. You need to set the JAVA_HOME environment variable. In DOS it is %JAVA_HOME% and in a Unix system, it is $JAVA_HOME. You also need to add %JAVA_HOME%/bin to the “path” environment variable. you can verify if Java is set up correctly by typing the following command in a command prompt.

Screen shot 2014-10-19 at 4.41.52 PM

If the version is not displayed, then Java is not set up correctly.

Install and configure Maven

Maven is a build and deployment tool.

Step 1: Download and install Maven from http://maven.apache.org/download.html.

Step 2: Configure the environment variables “M3_HOME” and “path”. For example

On DOS

On Unix

type the following in “.profile”

Once maven has been set up correctly, you should be able to verify it by

Screen shot 2014-10-19 at 4.44.25 PM

You can set up the repository location by opening the settings.xml file under conf folder inside your maven installation directory. Repository is where the dependency Java libraries will be stored.

<localRepository>c:\tools\home\.m2\repository</localRepository>

If your internet access requires proxy settings, you need have that added to your settings.xml file.


Install and configure eclipse. 

Step 1: Download and install eclipse latest version from http://www.eclipse.org/downloads/. It is an IDE (i.e. Integrated Development Environment). Get the EE (Enterprise Edition).

Step 2: It is a good practice to keep your eclipse work space separate from where your tutorials or projects are stored. This will not tie up your projects or tutorials with an IDE. What if you want to use your projects or tutorials with NetBeans? For example

Screen shot 2014-10-19 at 4.45.45 PM

Here is a simple step by step guide in setting up Maven and eclipse

Step 1: The settings.xml file under %M2_HOME%/conf is where you can set up your local repository location, central repository server from where you can download the relevant artifacts, proxy server settings to access internet, etc as shown below.


You can browse the repository by going to http://repo1.maven.org/maven2 where you can search for artifacts and relevant version numbers. You can even manually download artifacts.

Need to add the following snippets to the root element of your settings.xml file to tell where to download the jar files from.

Note: Drill down by groupid that specify in your pom.xml file under dependencies.

If you don’t configure the settings.xml, the defaults will be applied.

Maven Repo: where artifacts will be downloaded FROM: https://repo.maven.apache.org/maven2/

User Local Repo: where the artifacts will be downloaded TO: ${user.home}/.m2/repository. You can find this out on DOS or Unix with “env” or “set” commands respectively by listing the environment variables. Look for a variable like “HOME=/Users/xxxx”



Step 2: Generate the pom structure with the following maven command.

Note: Make sure that you already don’t have a folder “simple” under projects. Accept the default settings when prompted by pressing enter. Now, if you inspect the file structure, it will look like. “-B” is for batch mode, and you won’t be prompted for version numbers, etc. It will create the artifacts with the default version numbers.

Screen shot 2014-10-19 at 4.48.17 PM

Carefully, inspect the folders and files that maven has created. The local repository will also have a number of jar files for project dependencies like junit.jar and maven plugin jars.

Screen shot 2014-10-19 at 4.49.27 PM

This maven project can be imported into eclipse.

Step 3: create an eclipse short cut as shown below to open eclipse with your workspace “my-tutorials


Step 4: Close the welcome splash screen, and tell eclipse with the help of the m2e (i.e maven to eclipse) plugin where your maven settings.xml file is residing. This is done by selecting from the eclipse main menu Window –> Preferences –> Maven (note: you can also search at the top in the Preferences dialogue).  As you can see below, it will also display your repository location as highlighted in blue.

Screen shot 2014-10-19 at 4.50.44 PM

If m2e plugin is not installed

You won’t see the “Maven” in “Preferences”. Go to “Help” –> “Install New Software“, and install it from “http://download.eclipse.org/technology/m2e/releases”. Follow through the wizard and restart eclipse after installing the plugin. Now you should see “Maven” under “Preferences”.

Now, you can refresh your projects or select the project “simple” and then right-mouse-click to bring up the contextual menu, and then select “Maven –> Update Dependencies” to see the packages your project depends on. In this example, it is junit-xxx.jar.

Screen shot 2014-10-19 at 4.51.51 PM

You can also check you build path by  right-mouse-clicking on the simple project and then selecting Build Path –> Configure Build Path

Step 5: Import the maven project you had just created. Select  File –> Import and then

Screen shot 2014-10-19 at 4.52.59 PM

Click Next,

Screen shot 2014-10-19 at 4.54.21 PM

Step 6: Now, you can see the “App.java” Java source file as shown below. This was created by Maven.

Screen shot 2014-10-19 at 4.55.36 PM

Also inspect the pom.xml file which is used for packaging up your artifacts. The junit.jar is required for writing unit tests. You can also notice AppTest.java under src/test/java 


Screen shot 2014-10-19 at 4.56.55 PM

Step 7: You can run the class by “right-clicking” on App.java to bring up the contextual menu and then selecting “Run As” and then “Java Application“, which prints the output “Hello World” in the console tab window.

Step 8: You can now package it as a jar file by running the mvn command on a command prompt. The pom file says the packaging type is “jar”. Currently the target folder is empty, and once you package it, it will have the packaged artifacts.

Firstly, locate the folder where the pom.xml file is and then execute the mvn command.

Screen shot 2014-10-19 at 4.58.01 PM

The command is “mvn clean package“. if you refresh the “target” folder by selecting it and pressing F5 or by selecting it and then right-mouse-clicking and then selecting “Refresh” you will see the artifacts that are generated. If you don’t want the junit tests to be run, you can add -Dmaven.test.skip to the above command.

Screen shot 2014-10-19 at 4.59.31 PM

If you want the package to be deployed to your maven repository which is located at “c:\temp\java\.m2\repository” you should run the command “mvn clean install“.

Screen shot 2014-10-19 at 5.01.16 PM

Step 9: Finally, you can run the jar file from a command prompt.The classpath tells where to find the App.class file.

That’s it. You now have Java, Maven (build tool), and eclipse (IDE) working with each other. If you liked this give me your feedback and stay tuned for more tutorials.

Note: If you want to generate the eclipse related artifacts outside eclipse from a command-line, you can issue the following command mvn eclipse:clean eclipse:eclipse. Similar commands are available for other IDEs as well.

Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.

Posted in Setting up Tutorial
4 comments on “♥ 1. Setting up Java, Maven, and eclipse step by step
  1. anjaneyareddym says:

    Hi Arul,

    After removing the profile in setting.xml.archetype command is executed. Thanks.

    Regards,
    Anjaneya

  2. Arulkumaran says:

    1. Check your settings.xml file to ensure that your proxies and repositories are set up correctly. If you are behind a fire wall, you need to set up the proxy. The following is a dummy example. You need to fill in the correct proxy details.

    2. See if you can reach http://search.maven.org/ which is the maven repository. In the search box enter “org/apache/maven/plugins”. You should see “maven-archetype-plugin”.

    3. run your maven command with -X switch as it will give you more debugging statements as to where it is trying to download the plugin from.

  3. anjaneyareddym says:

    I did the same setting as above,But am getting below error.
    ownloading: http://search.maven.org/org/codehaus/mojo/maven-metadata.xml
    INFO] ————————————————————————
    INFO] BUILD FAILURE
    INFO] ————————————————————————
    INFO] Total time: 1.012s
    INFO] Finished at: Tue Mar 17 23:14:40 IST 2015
    INFO] Final Memory: 5M/15M
    INFO] ————————————————————————
    ERROR] No plugin found for prefix ‘archetype’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (c:\tools\home\.m2\repository), cen
    ERROR]
    ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    ERROR] Re-run Maven using the -X switch to enable full debug logging.
    ERROR]
    ERROR] For more information about the errors and possible solutions, please read the following articles:
    ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

    Please help on this

Leave a Reply

Your email address will not be published. Required fields are marked *

*

800+ Interview Q&As ♥Free|♦FAQ (Mouse Hover for Full Text)

open all | close all

200+ Java FAQs – Memory Joggers

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your "Career Skills"?

open all | close all