05E. Explain Java and Maven set up via Q&As

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:

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.

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.

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.

OR via the project specific “pom.xml” file

Q12. If I already have the jar file, can I manually install into my local Maven repository ?
A12. Yes, use the following command

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.

Screen shot 2014-10-19 at 5.13.36 PM


300+ Java Interview FAQs

Tutorials on Java & Big Data