Java Core Tutorialshttps://wwconfirmedw.codejava.net/java-coreWed, 15 May 2024 16:02:37 -0500Joomla! - Open Source Content Managementen-gbUnderstand 9 features of Java programming languagehttps://wwconfirmedw.codejava.net/java-core/features-of-the-java-programming-languagehttps://wwconfirmedw.codejava.net/java-core/features-of-the-java-programming-language

In this article, you will learn about the fundamental features of Java programming language. It’s like you need to learn the alphabet before learning how to read and write.

Generally, Java is a simple, robust and secure programming language. Here are the most important features of Java:

 

Features of Java

 

1. Java is Simple:

The Java programming language is easy to learn. Java code is easy to read and write.

 

2. Java is Familiar:

Java is similar to C/C++ but it removes the drawbacks and complexities of C/C++ like pointers and multiple inheritances. So if you have background in C/C++, you will find Java familiar and easy to learn.

 

3. Java is an Object-Oriented programming language:

Unlike C++ which is semi object-oriented, Java is a fully object-oriented programming language. It has all OOP features such as abstraction, encapsulation, inheritance and polymorphism.

 

4. Java supports Functional programming:

Since Java SE version 8 (JDK 8), Java is updated with functional programming feature like functional interfaces and Lambda Expressions. This increases the flexibility of Java.

 

5. Java is Robust:

With automatic garbage collection and simple memory management model (no pointers like C/C++), plus language features like generics, try-with-resources,… Java guides programmer toward reliable programming habits for creating highly reliable applications.

 

6. Java is Secure:

The Java platform is designed with security features built into the language and runtime system such as static type-checking at compile time and runtime checking (security manager), which let you creating applications that can’t be invaded from outside. You never hear about viruses attacking Java applications.

 

7. Java is High Performance:

Java code is compiled into bytecode which is highly optimized by the Java compiler, so that the Java virtual machine (JVM) can execute Java applications at full speed. In addition, compute-intensive code can be re-written in native code and interfaced with Java platform via Java Native Interface (JNI) thus improve the performance.

 

8. Java is Multithreaded:

The Java platform is designed with multithreading capabilities built into the language. That means you can build applications with many concurrent threads of activity, resulting in highly interactive and responsive applications.

 

9. Java is Platform Independence:

Java code is compiled into intermediate format (bytecode), which can be executed on any systems for which Java virtual machine is ported. That means you can write a Java program once and run it on Windows, Mac, Linux or Solaris without re-compiling. Thus the slogan “Write once, run anywhere” of Java.

Besides the above features, programmers can benefit from a strong and vibrant Java ecosystem:

  • Java is powered by Oracle - one of the leaders in the industry. Java also gets enormous support from big technology companies like IBM, Google, Redhat,… so it has been always evolving over the years.
  • There are a lot of open source libraries which you can choose for building your applications.
  • There are many superior tools and IDEs that makes your Java development easier.
  • There are many frameworks that help you build highly reliable applications quickly.
  • The community around Java technology is very big and mature, so that you can get support easily.

That’s why Java has been ranking as #1 programming language in the world by popularity, according to statistics by Tiobe Index.

So congratulate you on choosing Java as the programming language for developing awesome applications.

 

Next topic: How to write your first Java program

 

References:

Related Topics:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreMon, 13 Jul 2015 11:03:16 -0500
How to write, compile and run a hello world Java program for beginnershttps://wwconfirmedw.codejava.net/java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginnershttps://wwconfirmedw.codejava.net/java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners

If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have come to the right place. This tutorial will help you writing your first Java program, typically a “hello world” one - your first step of the adventure into Java programming world. Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.

To start, all you need is a fresh computer without any Java software installed, a text-based editor and a good internet connection.

NOTES:This beginner tutorial is targeted for Windows environment.

 

1. Download and install Java Development Kit

In order to write and run a Java program, you need to install a software program called Java SE Development Kit (or JDK for short, and SE means Standard Edition). Basically, a JDK contains:

    • JRE(Java Runtime Environment): is the core of the Java platform that enables running Java programs on your computer. The JRE includes JVM (Java Virtual Machine) that runs Java programs by translating from bytecode to platform-dependent code and executes them (Java programs are compiled into an intermediate form called bytecode), and other core libraries such as collections, File I/O, networking, etc.
    • Tools and libraries that support Java development.

The JDK ships with two powerful tools which every Java developer should be familiar with:

    • javac.exe: is Java compiler that translates programs written in Java code into bytecode form.
    • java.exe: is the Java Virtual Machine launcher that executes bytecode.

Click this link to download the latest version of JDK installer program:

Check the option “Accept License Agreement”, and choose an appropriate version for your computer from the list. Here we choose the version for Windows x64:

Download JDK

After downloading the program, run it to install the JDK on your computer (just following the steps, leave the defaults and click Next, Next…):

JDKSetup

You would see the JDK is installed in the following directory, for example: C:\Program Files\Java\jdk1.7.0_21. The following screenshot describes the JDK’s directory structure:

JDK directory structure

Now let’s test if Java runtime is installed correctly. Open a command prompt window and type:

java -version

You would see the following result:

test java version

That shows version of the JRE, e.g. “1.7.0_21” - Congratulations! Your computer is now capable of running Java programs.

Now try to type the following command:

javac -version

You would see the following error:

test javac version error

That’s because Windows could not find the javac program, so we need to set some environment variables which tell the location of javac.exe.

 

2. Set up environment variables

Now we’re going to set environment variables so that the javac.exe program can be accessed anywhere from command line. On Windows 7, go to My Computer and click System Properties:

click System Properties

Then click Advanced system settings:

click Advanced system settings

The System Properties dialog appears, select Advanced tab and click Environment Variables...:

System Properties dialog

The Environment Variable dialog appears, click on the New… button under the System variables section.

Environment Variables dialog

That opens up the New System Variable dialog. Type the following information:

New System Variable dialog

The field Variable name must be JAVA_HOME, and the field Variable value must point to JDK’s installation directory on your computer. Here it is set to c:\Program Files\Java\jdk1.7.0_21. Click OK to close this dialog.

Now back to the Environment Variables dialog, look for a variable called Path under the System Variables list, and click Edit…:

Edit Path variable

In the Edit System Variable dialog, append the following to the end of the field Variable value:

;%JAVA_HOME%\bin

Set JAVA HOME in Path variable

Note that there is a semicolon at the beginning to separate this value from other ones. Click OK three times to close all the dialogs.

Now we have to quit the current command prompt and open a new one to see the changes takes effect. Type the following command again in the re-opened command prompt window:

javac -version

You would see the following output:

test javac version success

Congratulations! You have completed the setup for essential Java development environment on your computer. It’s now ready to write your first Java program.

 

3. Code a Java hello world program

Open a simple text editor program such as Notepad and type the following content:

public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello world!");
	}
}

Save the file as HelloWorld.java (note that the extension is .java) under a directory, let’s say, C:\Java.

Don’t worry if you don’t understand everything in this simple Java code. The following picture explains it nicely:

Hello world program explanation

Every Java program starts from the main()method. This program simply prints “Hello world” to screen.

 

4. Compile your first Java program

Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command to change the current directory to the one where the source file is stored:

cd C:\Java

And type the following command:

javac HelloWorld.java

That invokes the Java compiler to compile code in the HelloWorld.java file into bytecode. Note that the file name ends with .java extension. You would see the following output:

compile HelloWorld program

If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the HelloWorld.class file which is bytecode form of the HelloWorld.java file. Now try to type dir in the command line, we’ll see the .class file:

see compiled bytecode file

So remember a Java program will be compiled into bytecode form (.class file).

 

5. Run your first Java program

It’s now ready to run our first Java program. Type the following command:

java HelloWorld

That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or .class extension). You would see the following output:

 run hello world program

It just prints out “Hello world!” to the screen and quits. Congratulations! You have successfully run your first Java program!

 

6. What we have learnt so far

Throughout this tutorial you have learnt the following things:

    • JDK is the Java SE Development Kit that contains tools and libraries for Java development.
    • JRE is the Java Runtime Environment that enables running Java programs on your computer.
    • JVM is the Java Virtual Machine that actually executes Java programs. With JVM, programs written in Java can run on multi-platforms (thus Java is called cross-platform language).
    • How to install JDK and configure environment variables.
    • javac is the Java compiler. It translates Java source code into bytecode.
    • java is the JVM launcher which we use to run our program.
    • Every Java program starts from the main() method.
    • When compiling, the compiler generates a .class file from a.java file.

 

You can also watch the video version of this tutorial:

https://www.youtube.com/watch?v=-KhuJT-qPwQ

Next, I recommend you to read this article: Understand Classes and Objects in Java

 

Related Java Hello World Tutorials:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreFri, 24 May 2013 01:07:53 -0500
How to set environment variables for Java using command linehttps://wwconfirmedw.codejava.net/java-core/how-to-set-environment-variables-for-java-using-command-linehttps://wwconfirmedw.codejava.net/java-core/how-to-set-environment-variables-for-java-using-command-line

Why do you need to setup environment variables in Java programming?

Well, in this article I talked about the JVM, JRE and JDK, which are the cornerstones of the Java programming language. You develop Java applications by using tools like editors, IDEs and servers. These tools need to use the Java compiler (javac) and Java launcher (java) to compile and run Java applications, but these tools don’t know where the JRE or JDK is.

So, by setting up the environment variables, you tell your tools that:

“Hey, you can find the compiler and launcher here and there”. 

The first thing you need to do after installing the JDK is creating an environment variable named JAVA_HOME and then update the PATH variable.

  • JAVA_HOME: stores location of the JDK’s installation directory. When you install development tools, they will first check for the JAVA_HOME variable. If found, they will stick with it. If not, they may ask you to manually specify the location where JRE/JDK is installed.

     

  • PATH: stores paths of directories where the operating system will look, to launch the requested programs quickly. For Java development, you should update this environment variable by adding an entry to the bin directory under JDK’s installation directory.

For example, if the JDK is installed at C:\Program Files\Java\jdk1.8.0, then you need to set:

  • JAVA_HOME = C:\Program Files\Java\jdk1.8.0
  • PATH = PATH + C:\Program Files\Java\jdk1.8.0\bin

Setting these environment variables on Windows is not difficult. Just go to Control Panel > System > Advanced system settings > Advanced > Environment Variables. You can see the whole steps to setup this stuff in the following article:

How to write, compile and run a hello world Java program for beginners

But that isn’t cool, because I’m about to show you how to do the same thing using command line prompt as shown below (Windows 7, 8 and 10):

setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx PATH "%PATH%;%JAVA_HOME%\bin";

The setx  command permanently updates the environment variables. To add/update system environment variables, you must use the -m switch and open the command prompt using Administrator privilege: Click Start, type cmd. When the cmd.exe icon appears, right click and select Run as administrator.

To add/update system environment variables:

setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";

I prefer setting the environment variables using this command-line alternative. It’s quick and easy instead of going through several dialogs like using the GUI.

To summary, here are some important points:

  • Always set JAVA_HOME when preparing development environment for Java.
  • JAVA_HOME points to the installation directory of JDK.
  • The PATH variable should contain an entry pointing to JAVA_HOME\bin.
  • Environment can be set either via the GUI or command line prompt.

I hope that you have learned something new today. You can also watch the following video for visual steps:

https://www.youtube.com/watch?v=nNKMplW-Tmw

 

Related Tutorials:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreTue, 07 Jul 2015 04:02:18 -0500
How to set JAVA_HOME environment variable on Windows 10https://wwconfirmedw.codejava.net/java-core/how-to-set-java-home-environment-variable-on-windows-10https://wwconfirmedw.codejava.net/java-core/how-to-set-java-home-environment-variable-on-windows-10

In this article, you will understand the details about JAVA_HOME environment variable and how to set it on Windows 10. 

What is JAVA_HOME?

By convention, JAVA_HOME is the name of an environment variable on the operating system that points to the installation directory of JDK (Java Development Kit) or JRE (Java Runtime Environment) – thus the name Java Home. For example:

JAVA_HOME = c:\Program Files\Java\jdk1.8.0_201

 

Why is JAVA_HOME needed?

To develop Java applications, you need to update the PATH environment variable of the operating system so development tools like Eclipse, NetBeans, Tomcat… can be executed because these programs need JDK/JRE to function. So the PATH environment variable should include JAVA_HOME:

PATH = Other Paths + JAVA_HOME

Other paths are set by various programs installed in the operating system. If the PATH environment variable doesn’t contain a path to JRE/JDK, a Java-based program might not be able to run. For example, typing java in the command prompt showing this error:

'java' is not recognized as an internal or external command, operable program or batch file.

error java command

Or Eclipse will fail to launch:

Eclipse launch error no Java home found

 

How to set JAVA_HOME on Windows 10

Here are the visual steps to properly set value for the JAVA_HOME and update the PATH environment variables in order to setup Java development environment on your computer:

1. Firstly, you need to identify the Java home directory, which is typically under C:\Program Files\Java directory. Open My Computer and navigate to this directory, you will see:

Java home directory

Here, the home of JDK is under C:\Program Files\Java\jdk1.80_201. The version number may vary, depending on the JDK you installed.

 

2. Open the System Environment Variables dialog by typing environment in the search area on Start menu. Click the suggested item Edit the system environment variables:

start menu open system environment variable dialog

The System Properties dialog appears, click the button Environment Variables.

System properties dialog

Then you will see this dialog:

Environment Variables dialog

 

3.Create the JAVA_HOME environment variable by clicking the New button at the bottom. In the New System Variable form, enter the name and value as follows:

New JAVA HOME system variable

 

Click OK, and you will see the JAVA_HOME variable is added to the list.

 

4.Update the PATH system variable. In the Environment Variables dialog, select the Path variable and click Edit:

Edit Path variable

Then in the Edit environment variable dialog, double click on the empty row just below the last text line, and enter %JAVA_HOME%\bin as follows:

Edit Path variable for Java home

The percent signs tell Windows that it refers to a variable – JAVA_HOME, and the \bin specifies the location of java.exe and javac.exe programs which are used to run and compile Java programs, as well as other tools in the JDK.

Click OK button to close all the dialogs, and you’re all set. Now you can open Eclipse or NetBeans to verify. Or open a command prompt and type in javac –version, you should see:

verify java home

NOTES:

You can add the path to the bin directory of Java home directly into the PATH variable. But it’s strongly recommend to follow the above steps to setup a JAVA_HOME variable because many Java programs depend on it.

When you installed a JDK, you might not need to update the system variable because the installer already did it for you.

 

Learn more:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreSat, 27 Apr 2019 02:07:07 -0500
What is Java?https://wwconfirmedw.codejava.net/java-core/what-is-javahttps://wwconfirmedw.codejava.net/java-core/what-is-java

Is Java an island of Indonesia archipelago? Yes, that's correct.

However, Java is very well known as a software platform, a computer programming language, and a fast growing and widely used technology worldwide.

The Java programming language was invented by James Gosling in 1995 and became a core component of the Java platform which is developed by Sun Microsystems Inc. (and now by Oracle Inc.). Today, Java is one of the most popular computer programming languages and computing platforms. Java is everywhere: on personal computers, enterprise servers, super computers, mobile phones, embedded devices... and even on Mars exploring robots.

 

What Java can do:

Well, Java can do everything you can imagine: allows playing games on mobile phones, chatting via webcam, rendering 3D models, processing banking transactions, controlling robots... and much more, here just to name a few.

 

What makes Java so popular:

Java has a number of advantages which make it a widespreading technology:

    • Platform independent: A Java program can run on any platform including Windows, Linux, Mac, Solaris, ... with a Java Virtual Machine installed on the target operating system. Developers write code once and the program can run anywhere, thus the very well know slogan "Write once, run anywhere".
    • Powerful language: The Java programming language is strong typed, has clear syntax and addresses some limitations of C/C++... that makes it a easy to learn language for programmers. There are thousands of libraries written in Java for almost any field.
    • Secure: Java has a built-in strong security manager from ground-up that prevents malicious code and viruses.
    • High performance: with the JVM has been improved over the years, Java applications are fast.
    • Networking: with built-in support for networking, developers can write internet-based and networked applications easily.

 

The big family of Java technologies:

Java is a really big technology which spreads across various platforms and devices. Instead of having a huge package for all platforms, the technology is devided into specific editions as following:

    • Java SE (Standard Edition): for desktop-based applications.
    • Java EE (Enterprise Edition): for enterprise web-based applications.
    • Java ME (Micro Edition): for mobile-based applications.
    • Java FX: for Rich Internet Applications (RIAs)
    • Java SE for Embedded Devices.

 

The Java logo:

The Java technology is very well known with its logo: a cup of hot coffee.

java logo

 

Related Tutorials:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreSun, 18 Mar 2012 22:08:48 -0500
What are JVM, JRE and JDK?https://wwconfirmedw.codejava.net/java-core/what-are-jvm-jre-and-jdkhttps://wwconfirmedw.codejava.net/java-core/what-are-jvm-jre-and-jdk

In this article, you will understand the differences among JVM, JRE and JDK. As a Java programmer, you should understand these basic stuffs clearly. It’s like you should see the characteristics and personality of your girlfriend or partner.

 

1. What is JVM?

JVM stands for Java Virtual Machine. It is an abstract computing machine that is responsible for executing Java programs. When you write a Java program, the source code is compiled into byte code which is understandable by the JVM. Upon execution, the JVM translates byte code into machine code of the target operating system.

The JVM is the cornerstone of the Java programming language. It is responsible for the very well-known feature of Java: cross-platform. That means you can write a Java program once and run it anywhere: Windows, Linux, Mac and Solaris, as long as JRE is installed on the host operating system.

Every time you run a Java program, the JVM is started to execute and manage the program’s execution. The JVM is running in two modes: client (default) and server.

An Oracle’s implementation for JVM is called Java HotSpot VM.

 

2. What is JRE?

JRE stands for Java Runtime Environment. It provides the libraries, JVM and other components necessary for you to run applets and applications written in the Java programming language.

The JRE contains standard tools such as java, keytool, policytool,… but it doesn’t contain compilers or debuggers for developing applets and applications.

When you deploy your Java applications on client’s computer, the client needs a JRE to be installed.

 

3. What is JDK?

JDK stands for Java Development Kit. It’s a superset of JRE. The JDK includes the JRE plus command-line development tools such as compilers (javac) and debuggers (jdb) and others (jar, javadoc, etc) that are necessary or useful for developing applets and applications.

Therefore, as a Java programmer, you have to install JDK as a minimum requirement for the development environment.

To summary:

  • JVM: is the virtual machine that runs Java applications. The JVM makes Java platform-independence.
  • JRE = JVM + standard libraries: provides environment for executing Java applications.
  • JDK = JRE + development tools for compiling and debugging Java applications.

The following picture depicts the relationship among JVM, JRE and JDK:

JVM JRE JDK

Okay, so far I have covered the basic information about the 3 cornerstones in Java programming: JVM, JRE and JDK. I hope you are now able to identify the differences among these components.

And here are some tips:

  • You should have both JRE and JDK installations (setup) on your computer. You will need both during the development process.
  • You should have multiple versions of JDK and JRE installed: 1.5, 1.6, 1.7 and 1.8 for different testing purposes in the future.
  • You should install both 32-bit and 64-bit versions.
  • When installing the JDK, remember to check ‘Install Demos and Samples’. Then you can explore various interesting examples in the demo directory under JDK’s installation path.
  • Only the JDK includes source code of the Java runtime libraries. You can discover the source code in the src.zip file which can be found under JDK’s installation directory.

 

4. Where to download JRE and JDK?

The starting point is java.oracle.com, but you can go directly to this: http://www.oracle.com/technetwork/java/javase/downloads/index.html.

 

5. Where to find further information?

Here are some official articles from Oracle for your reference:

That's the difference among JVM, JRE and JDK in Java. You will dive into these stuffs along the way of your Java programming journey. And today is good for the beginning.

 

Related Topics:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreTue, 07 Jul 2015 03:40:21 -0500
How to check Java versionhttps://wwconfirmedw.codejava.net/java-core/how-to-check-java-versionhttps://wwconfirmedw.codejava.net/java-core/how-to-check-java-version

This article describes some ways which you can use to check version of Java on your Windows computer. More exactly, the version of Java Runtime Environment (JRE) or Java Development Kit (JDK).

If you have OracleJDK installed on your computer, simply click menu Start, type about java into the search area, then click the About Java icon in the result. You can see the current version of Java in the following dialog:

about java version

Or you can go to Control Panel, switch to Large icons view and click the Java logo:

java in control panel

Then in the Java Control Panel dialog, click About button as shown in the following screenshot:

about java control panel

 

The second way (more familiar with programmers) to check Java version is opening a command prompt and type the following command:

java -version

Suppose that the PATH environment variable is updated for Java, you should see the following result:

check java version console

Here, the version of Java is 1.8.0, and 201 is the update number. Each version can have several update numbers - the higher the more recent it is.

Note that “Java(TM) SE Runtime Environment” meaning that you’re using Oracle JDK. In case you’re using OpenJDK, the output should be:

check java version openjdk

 

If JDK/JRE is not installed or somehow the PATH environment variable is not updated for Java, you can see the following message:

java is not recognized

 

Note that there can be multiple installations of JRE and/or JDK on a computer. In that case, the version of Java you see in the console is of the first JRE/JDK found in the list of environment variables.

To check the version exactly, you need to go to the bin directory of a JDK/JRE installation, for example:

cd C:\Program Files\Java\jdk1.8.0_201\bin

And type the java –version command.

 

https://www.youtube.com/watch?v=lwe5gMYugX4

 

Related Topics:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreWed, 10 Apr 2019 08:32:39 -0500
How to set JAVA_HOME in macOS and Linux permanentlyhttps://wwconfirmedw.codejava.net/java-core/set-java-home-in-macos-linuxhttps://wwconfirmedw.codejava.net/java-core/set-java-home-in-macos-linux

Setting up JAVA_HOME environment variable is the very first thing, which every programmer should know how to do in order to get started with Java development, as many Java-based applications, tools and libraries depend on JAVA_HOME.

In this article, I’d like to share with you guys, about how to setup JAVA_HOME environment variable on macOS / Linux operating system, with both Bash shell and Z-shell.

Basically, you can use the following commands in a terminal for setting JAVA_HOME and updating PATH environment variables:

export JAVA_HOME=$HOME/jdk-version/Contents/Home

export PATH=$JAVA_HOME/bin:$PATH

However, these commands have effect only in the current session. If you exit the terminal and open a new one, you will the changes lost.

So how to permanently set JAVA_HOME and update PATH environment variables on macOS / Linux? Read on to know how. And I suppose that you already have JDK (Java Development Kit) installed.

 

1. Set JAVA_HOME permanently in Z-shell

Z-shell (zsh) is the default shell in latest macOS operating systems. Firstly, you need to create a Z-shell resource file (.zshrc) in your user home directory, using the following command in terminal:

cat > .zshrc

Then type the following commands as content of the file:

export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home

export PATH=$JAVA_HOME/bin:$PATH

Note that the HOME environment variable refers to your user home directory. And I suppose that you have JDK installed in jdk-17.0.1.jdk directory, which is in your home directory.

Press Ctrl + D to save the file. The .zhsrc file is executed at shell’s startup, so every time you open a new terminal with Z-shell, the JAVA_HOME environment variable is set and PATH environment variable is updated.

Now you can type java -version or javac -version to verify. You should see the output looking something like this:

set java home on macos zsh

Now, quit the terminal. Then open a new one. Type java -version command again and you should see the change is permanent.

 


2. Set JAVA_HOME permanently in Bash shell

To setup JAVA_HOME with Bash shell, you create the Bash profile file (.bash_profile instead of .zshrc) in your user home directory, like this:

cat > .bash_profile

export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home

export PATH=$JAVA_HOME/bin:$PATH

set java home on macos bash

Press Ctrl + D to save the file. The .bash_profile file is executed at shell’s startup, so every time you open a new terminal with Bash shell, the JAVA_HOME environment variable is set and PATH environment variable is updated.

That’s how to set JAVA_HOME environment variable on macOS, with both Bash shell and Z-shell. It is also working on Linux.

https://www.youtube.com/watch?v=qotS3IXkJtQ

 

Related Articles:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreThu, 30 Dec 2021 07:39:17 -0600
Download and Install OpenJDK 17 on macOShttps://wwconfirmedw.codejava.net/java-core/install-openjdk-17-on-macoshttps://wwconfirmedw.codejava.net/java-core/install-openjdk-17-on-macos

In this article, I’d like to share with you the steps for downloading and installing Java Development Kit (JDK) with OpenJDK 17 on macOS. The same steps may be applied for any Unix operating systems.

You know, OpenJDK is a production-ready, open-source distribution of JDK. And JDK 17 is the latest Long Term Release (LTS) of Java SE platform, as of January 2022.

 

1. Download binary distribution of OpenJDK 17 for macOS

Visit the official download page of OpenJDK 17 at https://jdk.java.net/17, and click on the download link for macOS/x64 if your computer running on Intel’s CPU, or choose macOS/AArch64 for Apple’s CPU:

OpenJDK download page for macOS

It’s strongly recommend to verify SHA256 checksum of the downloaded file against the value published on the website (click the corresponding link sha256). Then open a new terminal window, and type the following command:

      shasum -a 256 openjdk-xxx.tar.gz

Compare the checksum value printed by the shasum command with the value published by the website. If both are identical, you can safely install the archive file.

Then extract the downloaded file .tar.gz to a directory on your computer, either using context menu provided by macOS (right click on the file, Open with > Archive Utility) or using the following command in a terminal session:

                tar -xf openjdk-xxx.tar.gz -C $HOME/OpenJDK

This unzips the archive file to the directory OpenJDK in your user home directory. Explore the extracted directory, and you will see the Java home folder would be $HOME/OpenJDK/jdk-17.0.1.jdk/Contents/Home


2. Install OpenJDK 17 on macOS

Next, you need to set JAVA_HOME and update PATH environment variables so other Java programs and tools could find JDK. In the terminal, type the following command to create the Z-Shell resource file:

cat > .zshrc

export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home

export PATH=$JAVA_HOME/bin:$PATH

Press Control + D to save the file. The .zshrc file is run whenever a new terminal session is initialized, which sets JAVA_HOME and update PATH environment variables.

NOTE: If you’re using Bash shell, create the .bash_profile file instead of .zshrc file.

Quit the terminal and start a new session. Then type java -version command, you should see the following output:

java version on macOS

That means you have successfully installed OpenJDK on your Mac. To see the steps in action, watch the following video:

https://www.youtube.com/watch?v=zwCI3p7Eu8c

 

Related Articles:

]]>
hainatu@gmail.com (Nam Ha Minh)Java CoreTue, 11 Jan 2022 02:51:45 -0600