This is an extension posts outlining setting up & getting started with Java & Maven.
Q1. What is the difference between JDK & JRE?
A1. JDK: You can download a copy of the Java Development Kit (JDK) for your operating system like Windows 64 bit, Mac OS X, Linux X64, Solaris X64, etc.
JRE: Java Runtime Environment is an implementation of the JVM. The JDK typically includes the Java Runtime Environment (JRE) which contains the virtual machine and other dependencies to run Java applications. If you just only want to run Java applications without any development work, then you can download the JRE for your operating system like Windows 64 bit, Mac OS X, Linux X84, Solaris X64, etc.
Q2. How does Java’s WORA (i.e. Write Once Run Anywhere) work?
A2. You can develop a Java application as a “xxx.jar” file in Windows 64 bit OS, which essentially contains “*.class” files compiled with java.exe shipped with the JDK for “Windows 64”. This “xxx.jar” file can be deployed & run in a Linux box running “JRE for Linux X64”. The “JRE for Linux X64” will read the “*.class” files in byte code created with “JDK Windows 64 bit”, and convert the byte code to Linux machine code so that the Linux OS will understand.
Q3. What are environment variables?
A3. An environment variable defines some aspect of a user’s or a program’s environment. After you have installed the JDK or JRE in Windows, you must set the JAVA_HOME or JRE_HOME environment variables to point to the installation directory. Same applies for other applications like Maven. These variables are then used to set the “PATH” variable so that when you type “java” or “mvn” in a DOS command-line, the application can be found.
In a Windows platform, you can write a batch file to set up your environment variables as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@echo off REM This is sample to set up your Java and Maven environment variables SET TOOLS_HOME=C:\tools SET USERPROFILE=%TOOLS_HOME%\home SET JAVA_HOME=%TOOLS_HOME%\java\jdk1.6.0_45 SET M3_HOME=%TOOLS_HOME%\maven\apache-maven-3.1.0 SET MAVEN_OPTS=-Xmx512m -Duser.home=%USERPROFILE% SET PATH=%PATH%;%M3_HOME%\bin;%JAVA_HOME%\bin echo ******** Java Toolkit setup **************** echo USERPROFILE = %USERPROFILE% echo JAVA_HOME = %JAVA_HOME% echo M3_HOME = %M3_HOME% echo PATH = %PATH% echo ******************************************** |
Q4. How do you list the environment variables on DOS command prompt?
A4 By typing the “set” command.
Q5. How do you list the environment variables on Unix command prompt?
A5 By typing the “env” command.
Q6. How do echo a particular environment variable?
A6. Typing the “echo %M3_HOME%” command displays “M3_HOME” variable. In Unix use “echo $M3_HOME”.
Q7. How do you find out in which folder an application like Java or Maven is installed?
A7. In DOS, use “where” command as in “where java”, “where mvn”, etc. In Unix, use “which” command as in “which java”, “which mvn”, etc.
Q8. How will you set your environment variables on an account or System level on Windows 8 or later?
A8. On Windows 8, search for “Edit environment variables”.
Then, you can set it via the pop up GUI
Q9. Maven uses a settings.xml file for its configuration. How do you find out which settings.xml file is being used by Maven?
A9. With the debug option mvn -X command.
|
1 2 3 4 5 |
[INFO] Error stacktraces are turned on. [DEBUG] Reading global settings from C:\TOOLS\maven\apache-maven-3.1.0\bin\..\conf\settings.xml [DEBUG] Reading user settings from C:\Users\akumaras\.m2\settings.xml [DEBUG] Using local repository at c:\tools\home\.m2\repository [DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for c:\tools\home\.m2\repository |
You can get both the settings.xml file and the repository location “c:\tools\home\.m2\repository” where the artefacts will be downloaded to.
Maven uses the user.home property that can be set via MAVEN_OPTS environment property to set JVM heap memory.
|
1 2 |
SET MAVEN_OPTS=-Xmx512m -Duser.home=%USERPROFILE% |
Q10. How do you know that your Java and Maven are set up correctly?
A10. Typing “java” or “mvn” on a command prompt should recognize the command.
Q11. How does Maven know where to download the library jar files from? In other words, where do you specify the “Maven central repository URL”?
A11. Via the global “settings.xml file or project specific “pom.xml” file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> .... <profiles> <profile> <id>securecentral</id> <!--Override the repository (and pluginRepository) "central" from the Maven Super POM --> <repositories> <repository> <id>central</id> <url>http://repo1.maven.org/maven2</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://repo1.maven.org/maven2</url> <releases> <enabled>true</enabled> </releases> </pluginRepository> </pluginRepositories> </profile> </profiles> ... </settings> |
OR via the project specific “pom.xml” file
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<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/maven-v4_0_0.xsd"> ... <repositories> <repository> <id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url> </repository> </repositories> ... </project> |
Q12. If I already have the jar file, can I manually install into my local Maven repository ?
A12. Yes, use the following command
|
1 2 3 4 5 6 7 8 |
mvn install:install-file \ -DgroupId=org.springframework \ -DartifactId=spring-core \ -Dpackaging=jar \ -Dversion=3.1.0.RELEASE \ -Dfile=jta-1.0.1B.jar \ -DgeneratePom=true |
Q13. How do you search for artifacts on the central repository?
A13. Go to central repository website http://search.maven.org/ and search. Prefix with g: for groupid, a: for artifactid, and v: for version number.
|
1 2 |
g:org.springframework a:spring-core v:3.1.0.RELEASE |


