Saturday, November 15, 2014

Getting Started with Java's Hello World: Part II

Install Maven

This post is continued from Getting Started with Java's Hello World: Part I. In this section, we will download Maven 3 for Windows. Maven is typically used to compile Java programs. I googled "download Maven for Windows", clicked on Download - Maven - Download Apache Maven (Apache), went to the Maven 3.1.0 Section, clicked on apache-maven-3.1.0-bin.zip.
  1. Download the zip file to your Desktop.
  2. Extract the file to "C:\".
This created the "C:\apache-maven-3.1.0" directory. To verify that you can run Maven:
  1. Open a command prompt.
  2. Type the following command: "C:\apache-maven-3.1.0\bin\mvn"
This gave me an "ERROR: JAVA_HOME not found in your environment.". To configure Maven to use Java 7:
  1. Run->Type "sysdm.cpl"->Advanced Tab->Environment Variables->System Variables Section.
  2. New->Enter "JAVA_HOME" as the name and "C:\Program Files\Java\jdk1.7.0_25" as the value.
  3. Select the Path variable->Edit.
  4. Use the error keys to move to the end of the value field and add ";C:\apache-maven-3.1.0\bin" (Make sure the first character is a semicolon, so that you can run maven from the command line, so you don't get a "mvn is not a recognized command" error).
  5. Click OK to close the System Properties Control Panel.
To see that Maven was properly configured running from "C:\apache-maven-3.1.0\bin" and with Java 7 (1.7.0_25):
  1. Close the existing command prompt.
  2. Open a new command prompt.
  3. Type the following command: mvn -version
The command prompt should show you the Maven Version you just installed along with the Java Version you just installed.

Create Your Maven Project

In this section we will create a Java Project. We will use Maven to compile this project from the pom.xml file. See http://maven.apache.org/guides/getting-started/ for more details.
  1. Open a command prompt.
  2. Create a workspace directory (ie Type: mkdir "C:\Users\Scott\workspace").
  3. Move to your workspace directory (ie Type: cd "C:\Users\Scott\workspace").
  4. Type the following command: mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app
  5. When you see "Choose a number or apply filter", Hit "Enter".
  6. When you see "Choose a number", type "6" and Hit "Enter".
  7. When you see "Define value for property", type "1.0" and Hit "Enter".
  8. When you see "Y", type "Y" and Hit "Enter".
    • This will download a bunch of libraries (jars) to the Local Maven Repository, ie "C:\Users\Scott\.m2\repository"
    • This will also create the my-app project (ie "C:\Users\Scott\workspace\my-app\pom.xml")
    • This will also create a src directory (ie "C:\Users\Scott\workspace\my-app\src")
  9. Type: cd my-app
  10. Type: mvn clean
    • This will clean the project directory.
    • You should see BUILD SUCCESS.
    • This command looks for the pom.xml file which is used to manage files for a Java Project
  11. Type: mvn compile
    • This will compile the project.
    • You should see BUILD SUCCESS.
    • This will create compiled Java class files (ie "C:\Users\Scott\workspace\my-app\target\classes").
    • You should be able to run the compiled Java class file: (ie to see "Hello World!" displayed, Type: java -classpath "C:\Users\Scott\workspace\my-app\target\classes" com.mycompany.app.App).
  12. Type: mvn test
    • This will run tests in the project (currently 1 test).
    • You should see BUILD SUCCESS.
  13. Type: mvn eclipse:eclipse
    • This will create Eclipse project files.
    • You should see BUILD SUCCESS.
  14. Type the following command: mvn eclipse:add-maven-repo -Declipse.workspace="C:\Users\Scott\workspace"
    • Replace "C:\Users\Scott\workspace" with your workspace.
    • This sets up a Java classpath variable which allows Eclipse to find the local Maven repository.
Note: You may edit the App.java file directly with Notepad to change the output. Continue onto Getting Started with Java's Hello World: Part III

This post was reposted from http://scottizu.wordpress.com/2013/08/14/getting-started-with-javas-hello-world-part-ii/, originally written on August 14th, 2013.

No comments:

Post a Comment