For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
Java is an agile, object-oriented programming language. It helps developers write code once and run it on any platform. It provides a robust ecosystem. This includes a virtual machine (JVM), libraries, and tools. This makes it popular for building various applications, from web and mobile to enterprise systems.
This tutorial teaches how to set up Java home and IDE on macOS.
In this tutorial, we will mainly deal with areas such as setting up JAVA_HOME and the IDE on macOS, the difference between JDK, JRE, and JVM, the first Java program on a Mac, and using a terminal via homebrew, among others.
The JVM (Java Virtual Machine) is responsible for executing Java programs. It takes the compiled bytecode. This is generated by the Java compiler (JAVAC). Then the JVM converts it into machine code. This is done to understand and execute it by the underlying hardware. The JVM is integral to the JDK (Java Development Kit) and the JRE (Java Runtime Environment). It performs various functions, such as memory management and security.
The JDK is a comprehensive package. This includes all the tools necessary for developing Java applications. It encompasses the compiler (JAVAC), the JRE, debugging tools, documentation (JavaDocs), and other utilities. The JDK provides a complete development environment. It allows programmers to write, compile, and test their Java code.
The JRE is a subset of the JDK. It is solely focused on running Java applications. It comprises the JVM, class libraries, and other resources required to execute Java bytecode. The JRE does not include development tools like the compiler or debugger. Its primary purpose is to provide a runtime environment for running Java programs.
JDK is essential for both compiling and running Java programs. It includes the necessary development tools and the JRE, encompassing the JVM and libraries for executing Java bytecode. The JRE, on its own, allows the execution of Java programs but lacks the development tools required for application development.
Here is a comprehensive list of differences between JDK, JRE, and JVM:
JDK (Java Development Kit) | JRE (Java Runtime Environment) | JVM (Java Virtual Machine) | |
Definition | Comprehensive package for developing Java applications | A subset of JDK focused on running Java applications | Runtime environment responsible for executing Java bytecode |
Components | Compiler (JAVAC), debugger, JavaDocs, libraries, development tools | JVM, libraries, applet support, plugins | Interpreter and runtime system for executing Java bytecode |
Purpose | Provides tools and resources for developing Java programs | Allows running Java programs | Converts bytecode into machine code and manages program execution |
Development Tools | Yes (includes compiler, debugger and other utilities) | No (focused on the runtime environment) | No (focused on the runtime environment) |
Execution | Can compile and run Java programs | Can only run Java programs | Executes compiled bytecode on the target hardware |
Platform-Dependent | Yes, different JDK versions for different platforms | Yes, JREs specific to each platform | Yes, JVM implementation varies for different platforms |
Usage | Required for both development and execution of Java programs | Only required for executing Java programs | Only required for executing Java programs |
Example | JDK 17 | JRE 17 | JVM (HotSpot, OpenJ9, etc.) |
There are two ways to download Java for Mac:
Here are the steps to download JDK 17 from the Oracle website:
(Using javac -version)
(Java Version)
/usr/libexec/java_home -v{YOUR_VERSION}
(We must replace ‘YOUR_VERSION’ with the version number of your JDK installation. For example, if you are using JDK 17.0.7, the command would be:
/usr/libexec/java_home -v17
(Visual Studio)
We must first create a new file from VS and name it .bash_profile. Once we have opened the file, we have to use the following command:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-{YOUR_VERSION}.jdk/Contents/Home
(For example, if you have JDK 18.0.1.1, it will be …/jdk-18.0.1.1.jdk/…)
( You can also use the code ~/. Bash_profile command if you are using the terminal)
We now have to refresh our environment file by using this command:
source ~/.bash_profile
And then, we finally And echo the JAVA_HOME variable with this command:
echo $JAVA_HOME
Using Homebrew is another way we can install Java on macOS. Let us explore how to install OpenJDK on macOS using the terminal via Homebrew.
Homebrew is a popular package manager for macOS. It lets you easily install and manage software packages from the command line. To install Homebrew, open Terminal and enter the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
You will be asked for your password and then you will be asked to press return/enter to confirm the installation of the homebrew files.
The directories will start getting created and the Command Line Tools for Xcode will get installed.
The downloading and installation process will take a bit of time and thus we have to be patient. Once the installation is done, Homebrew is ready to be used.
In order to install OpenJDK, we must now use this command:
brew install openjdk@11
You will get this error message if you already have OpenJDK installed, otherwise, OpenJDK will be installed in your system and you can finally verify the installation with “java -version”.
To change the path of Java in your system, you need to open the Bash profile. The Bash profile is a script that runs every time you start a new Terminal session. Here is the command for opening the Bash profile in Terminal:
nano ~/.bash_profile
(This command will open the Bash profile in the Nano text editor.)
Then, we add the following line to set the Java path in the Nano editor:
export JAVA_HOME="/usr/local/opt/openjdk@17"
This line sets the JAVA_HOME environment variable to the path where OpenJDK is installed. Replace 17 with the appropriate Java version number if you installed a different version.
Finally, after adding the line, press Ctrl + X to exit Nano. It will prompt you to save the changes, so press Y and Enter to save the file.
Once we are done with the setup, we must refresh the bash profile. To apply the changes, run the following command in Terminal:
source ~/.bash_profile
We have now successfully set/changed the Java path on macOS.
To install IntelliJ IDEA, we can follow these steps:
To install Eclipse, we can follow these steps:
To write the first program on Mac, we must first use a text editor or a code editor to write a Java program.
Here is a simple Java program for displaying the sentence “upGrad teaches programming.”:
public class upGradTutorials {
public static void main(String[] args) {
System.out.println("upGrad teaches programming.");
}
}
We then save the file as upGradTutorials.java in our target directory.
Finally, we open the terminal and navigate to the directory where you saved the file using the cd command. We can then compile the Java program by using the command javac upGradTutorials.java. Once the compilation is successful, execute the program with the command java upGradTutorials.
You should see the output "upGrad teaches programming." displayed in the Terminal.
Setting up Java Home and IDE on macOS is fairly simple. This tutorial has very clearly elaborated on the process of installation. Mastering Java will be easier with the help of a certified course. If you want to be a software developer, you could up courses with credible learning platforms such as upGrad. The courses on upGrad are designed to make you ready for the industry. Visit upGrad to learn more.
1. Can I have more than one version of Java installed on my Mac?
Yes, you can have more than one version of Java installed on your Mac. However, you must set the desired version as the default Java version. This has to be explicitly specified in your development environment.
2. Can I set the default Java version on my Mac?
You can use the “java_home” command to set the default Java version. Open the Terminal application. Run the command: “export JAVA_HOME=$(/usr/libexec/java_home)”. This sets the default Java version to the latest installed version.
3. Can I uninstall older versions of Java from my Mac?
Yes, you can uninstall older versions of Java from your Mac if required. Go to the "Applications" folder. Look for folders or files related to older Java versions. Move them to the Trash. Additionally, you can use the "Java Preferences" utility (located in the Utilities folder) to manage and uninstall Java versions.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.