Java Core TutorialsFree and quality Java programming tutorials, articles, guides, code examples, sample projects and best practiceshttps://codejava.net/java-core2024-05-14T02:55:00-05:00Joomla! - Open Source Content ManagementUnderstand 9 features of Java programming language2015-07-13T11:03:16-05:002015-07-13T11:03:16-05:00https://codejava.net/java-core/features-of-the-java-programming-languageNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>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.</p> <p>Generally, Java is a simple, robust and secure programming language. Here are the most important features of Java:</p> <p>&nbsp;</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/Features_of_Java.png" alt="Features of Java" width="540" height="399" /></p> <h2>&nbsp;</h2> <h2><strong>1. Java is Simple: </strong></h2> <p>The Java programming language is easy to learn. Java code is easy to read and write.</p> <p>&nbsp;</p> <h2><strong>2. Java is Familiar: </strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>3. Java is an Object-Oriented programming language:</strong></h2> <p>Unlike C++ which is semi object-oriented, Java is a fully object-oriented programming language. It has all OOP features such as <a href="java-core/the-java-language/what-is-abstraction-in-java-the-why-and-the-truth" target="_blank">abstraction</a>, <a href="java-core/the-java-language/what-is-encapsulation-in-java-the-what-why-and-how" target="_blank">encapsulation</a>, <a href="java-core/the-java-language/what-is-inheritance-in-java-the-what-why-and-how" target="_blank">inheritance</a> and <a href="java-core/the-java-language/what-is-polymorphism-in-java-the-what-how-and-why" target="_blank">polymorphism</a>.</p> <p>&nbsp;</p> <h2 style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #333333;"><strong>4. Java supports Functional programming:</strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>5. Java is Robust: </strong></h2> <p>With automatic garbage collection and simple memory management model (no pointers like C/C++), plus language features like <a href="java-core/collections/what-are-generics-in-java" target="_blank">generics</a>, <a href="java-core/the-java-language/using-try-with-resources-examples-java-7" target="_blank">try-with-resources</a>,… Java guides programmer toward reliable programming habits for creating highly reliable applications.</p> <p>&nbsp;</p> <h2><strong>6. Java is Secure:</strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>7. Java is High Performance: </strong></h2> <p>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 <i>Java Native Interface</i> (JNI) thus improve the performance.</p> <p>&nbsp;</p> <h2><strong>8. Java is Multithreaded:</strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>9. Java is Platform Independence:</strong></h2> <p>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 “<i>Write once, run anywhere</i>” of Java.</p> <p>Besides the above features, programmers can benefit from a strong and vibrant Java ecosystem:</p> <ul> <li>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.</li> <li>There are a lot of open source libraries which you can choose for building your applications.</li> <li>There are many superior tools and IDEs that makes your Java development easier.</li> <li>There are many frameworks that help you build highly reliable applications quickly.</li> <li>The community around Java technology is very big and mature, so that you can get support easily.</li> </ul> <p>That’s why Java has been ranking as #1 programming language in the world by popularity, according to <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html" rel="nofollow" target="_blank">statistics by Tiobe Index</a>.</p> <p>So congratulate you on choosing Java as the programming language for developing awesome applications.</p> <p>&nbsp;</p> <p><strong>Next topic:</strong>&nbsp;<a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write your first Java program</a></p> <p>&nbsp;</p> <h3>References:</h3> <ul> <li><a href="http://www.oracle.com/technetwork/java/intro-141325.html" rel="nofollow" target="_blank">The Java Language Environment</a></li> <li><a href="http://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html" rel="nofollow" target="_blank">Java Programming Language Enhancements</a></li> <li><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"><a href="http://docs.oracle.com/javase/specs/jls/se8/html/index.html" rel="nofollow" target="_blank">The Java Language Specification</a></span></li> </ul> <p><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"></span></p> <h3><span style="font-family: 'Open Sans', sans-serif; font-size: 18px;">Related Topics:</span></h3> <p><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"></span></p> <ul style="font-family: Arial, Helvetica, sans-serif; font-size: 14.3px;"> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-se/java-se-versions-history" target="_blank">Java versions history</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul> <p><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"></span></p></div><div class="feed-description"><p>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.</p> <p>Generally, Java is a simple, robust and secure programming language. Here are the most important features of Java:</p> <p>&nbsp;</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/Features_of_Java.png" alt="Features of Java" width="540" height="399" /></p> <h2>&nbsp;</h2> <h2><strong>1. Java is Simple: </strong></h2> <p>The Java programming language is easy to learn. Java code is easy to read and write.</p> <p>&nbsp;</p> <h2><strong>2. Java is Familiar: </strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>3. Java is an Object-Oriented programming language:</strong></h2> <p>Unlike C++ which is semi object-oriented, Java is a fully object-oriented programming language. It has all OOP features such as <a href="java-core/the-java-language/what-is-abstraction-in-java-the-why-and-the-truth" target="_blank">abstraction</a>, <a href="java-core/the-java-language/what-is-encapsulation-in-java-the-what-why-and-how" target="_blank">encapsulation</a>, <a href="java-core/the-java-language/what-is-inheritance-in-java-the-what-why-and-how" target="_blank">inheritance</a> and <a href="java-core/the-java-language/what-is-polymorphism-in-java-the-what-how-and-why" target="_blank">polymorphism</a>.</p> <p>&nbsp;</p> <h2 style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #333333;"><strong>4. Java supports Functional programming:</strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>5. Java is Robust: </strong></h2> <p>With automatic garbage collection and simple memory management model (no pointers like C/C++), plus language features like <a href="java-core/collections/what-are-generics-in-java" target="_blank">generics</a>, <a href="java-core/the-java-language/using-try-with-resources-examples-java-7" target="_blank">try-with-resources</a>,… Java guides programmer toward reliable programming habits for creating highly reliable applications.</p> <p>&nbsp;</p> <h2><strong>6. Java is Secure:</strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>7. Java is High Performance: </strong></h2> <p>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 <i>Java Native Interface</i> (JNI) thus improve the performance.</p> <p>&nbsp;</p> <h2><strong>8. Java is Multithreaded:</strong></h2> <p>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.</p> <p>&nbsp;</p> <h2><strong>9. Java is Platform Independence:</strong></h2> <p>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 “<i>Write once, run anywhere</i>” of Java.</p> <p>Besides the above features, programmers can benefit from a strong and vibrant Java ecosystem:</p> <ul> <li>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.</li> <li>There are a lot of open source libraries which you can choose for building your applications.</li> <li>There are many superior tools and IDEs that makes your Java development easier.</li> <li>There are many frameworks that help you build highly reliable applications quickly.</li> <li>The community around Java technology is very big and mature, so that you can get support easily.</li> </ul> <p>That’s why Java has been ranking as #1 programming language in the world by popularity, according to <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html" rel="nofollow" target="_blank">statistics by Tiobe Index</a>.</p> <p>So congratulate you on choosing Java as the programming language for developing awesome applications.</p> <p>&nbsp;</p> <p><strong>Next topic:</strong>&nbsp;<a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write your first Java program</a></p> <p>&nbsp;</p> <h3>References:</h3> <ul> <li><a href="http://www.oracle.com/technetwork/java/intro-141325.html" rel="nofollow" target="_blank">The Java Language Environment</a></li> <li><a href="http://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html" rel="nofollow" target="_blank">Java Programming Language Enhancements</a></li> <li><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"><a href="http://docs.oracle.com/javase/specs/jls/se8/html/index.html" rel="nofollow" target="_blank">The Java Language Specification</a></span></li> </ul> <p><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"></span></p> <h3><span style="font-family: 'Open Sans', sans-serif; font-size: 18px;">Related Topics:</span></h3> <p><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"></span></p> <ul style="font-family: Arial, Helvetica, sans-serif; font-size: 14.3px;"> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-se/java-se-versions-history" target="_blank">Java versions history</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul> <p><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif';"></span></p></div>How to write, compile and run a hello world Java program for beginners2013-05-24T01:07:53-05:002013-05-24T01:07:53-05:00https://codejava.net/java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginnersNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>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.</p> <p>To start, all you need is a fresh computer without any Java software installed, a <a href="coding/how-to-compile-and-run-a-java-program-with-sublime-text-3" target="_blank">text-based editor</a> and a good internet connection.</p> <p><span style="font-size: 12pt;"><strong><span style="line-height: 115%;">NOTES:</span></strong></span><span style="font-size: 14pt; line-height: 115%;"> </span>This beginner tutorial is targeted for Windows environment.</p> <center></center> <p>&nbsp;</p> <h2>1. Download and install Java Development Kit</h2> <p>In order to write and run a Java program, you need to install a software program called <em>Java SE Development Kit</em> (or JDK for short, and SE means Standard Edition). Basically, a JDK contains:</p> <ul> <li style="list-style-type: none;"> <ul> <li><strong>JRE</strong><em>(Java Runtime Environment)</em>: is the core of the Java platform that enables running Java programs on your computer. The JRE includes <strong>JVM&nbsp;</strong><em>(Java Virtual Machine)</em> 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.</li> <li>Tools and libraries that support Java development.</li> </ul> </li> </ul> <p>The JDK ships with two powerful tools which every Java developer should be familiar with:</p> <ul> <li style="list-style-type: none;"> <ul> <li><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">javac.exe</span>: is Java compiler that translates programs written in Java code into bytecode form.</li> <li><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">java.exe</span>: is the Java Virtual Machine launcher that executes bytecode.</li> </ul> </li> </ul> <p>Click <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html" rel="nofollow" target="_blank">this link</a> to download the latest version of JDK installer program:</p> <p>Check the option “<strong>Accept License Agreement</strong>”, and choose an appropriate version for your computer from the list. Here we choose the version for Windows x64:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Download%20JDK.png" alt="Download JDK" width="565" height="464" /></p> <p>After downloading the program, run it to install the JDK on your computer (just following the steps, leave the defaults and click Next, Next…):</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/JDKSetup.png" alt="JDKSetup" width="689" height="543" /></p> <p>You would see the JDK is installed in the following directory, for example: <i>C:\Program Files\Java\jdk1.7.0_21</i>. The following screenshot describes the JDK’s directory structure:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/JDK%20directory%20structure.png" alt="JDK directory structure" width="483" height="352" /></p> <p>Now let’s test if Java runtime is installed correctly. Open a command prompt window and type:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">java -version</span> </strong> </span></p> <p>You would see the following result:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/test%20java%20version.png" alt="test java version" width="508" height="89" /></p> <p>That shows version of the JRE, e.g. “1.7.0_21” - Congratulations! Your computer is now capable of running Java programs.</p> <p>Now try to type the following command:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac -version</span> </strong> </span></p> <p>You would see the following error:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/test%20javac%20version%20error.png" alt="test javac version error" width="494" height="78" /></p> <p>That’s because Windows could not find the <a href="java-core/tools/using-javac-command" target="_blank">javac</a><span style="font-size: 10pt; line-height: 115%;">&nbsp;</span>program, so we need to set some environment variables which tell the location of <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">javac.exe</span>.</span></p> <p>&nbsp;</p> <h2>2. Set up environment variables</h2> <p>Now we’re going to set environment variables so that the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">javac.exe</span>&nbsp;</span>program can be accessed anywhere from command line. On Windows 7, go to My Computer and click <strong>System Properties</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/click%20System%20Properties.png" alt="click System Properties" width="233" height="93" /></p> <p>Then click <strong>Advanced system settings</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/click%20Advanced%20system%20settings.png" alt="click Advanced system settings" width="187" height="208" /></p> <p>The <i>System Properties </i>dialog appears, select <i>Advanced</i> tab and click <strong>Environment Variables...</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/System%20Properties%20dialog.png" alt="System Properties dialog" width="426" height="474" /></p> <p>The <i>Environment Variable</i> dialog appears, click on the <strong>New…</strong> button under the <i>System variables</i> section.</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Environment%20Variables%20dialog.png" alt="Environment Variables dialog" width="394" height="436" /></p> <p>That opens up the <i>New System Variable</i> dialog. Type the following information:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/New%20System%20Variable%20dialog.png" alt="New System Variable dialog" width="357" height="153" /></p> <p>The field <i>Variable name</i> must be <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span>, and the field <i>Variable value</i> must point to JDK’s installation directory on your computer. Here it is set to <i>c:\Program Files\Java\jdk1.7.0_21</i>. Click <strong>OK</strong> to close this dialog.</p> <p>Now back to the <i>Environment Variables</i> dialog, look for a variable called <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">Path</span></strong> under the <i>System Variables</i> list, and click <strong>Edit…</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Edit%20Path%20variable.png" alt="Edit Path variable" width="374" height="174" /></p> <p>In the <i>Edit System Variable</i> dialog, append the following to the end of the field <i>Variable value</i>:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">;%JAVA_HOME%\bin</span> </strong> </span></p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Set%20JAVA_HOME%20in%20Path%20variable.png" alt="Set JAVA HOME in Path variable" width="357" height="153" /></p> <p>Note that there is a semicolon at the beginning to separate this value from other ones. Click <strong>OK</strong> three times to close all the dialogs.</p> <p>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:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac -version</span> </strong> </span></p> <p>You would see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/test%20javac%20version%20success.png" alt="test javac version success" width="163" height="74" /></p> <p>Congratulations! You have completed the setup for essential Java development environment on your computer. It’s now ready to write your first Java program.</p> <p>&nbsp;</p> <h2>3. Code a Java hello world program</h2> <p>Open a simple text editor program such as Notepad and type the following content:</p> <pre class="brush:java;toolbar:false">public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } } </pre> <p>Save the file as <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.java</span><span style="font-size: 10pt; line-height: 115%;"> (note that the extension is </span><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.java</span><span style="font-size: 10pt; line-height: 115%;">) </span>under a directory, let’s say, <i>C:\Java</i>.</p> <p>Don’t worry if you don’t understand everything in this simple Java code. The following picture explains it nicely:</p> <p><img loading="lazy" src="images/articles/javacore/beginner/Hello%20world%20program%20explanation.png" alt="Hello world program explanation" width="695" height="355" /></p> <p>Every Java program starts from the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">main()</span><span style="font-size: 10pt; line-height: 115%;"> </span>method. This program simply prints “Hello world” to screen.</p> <p>&nbsp;</p> <h2>4. Compile your first Java program</h2> <p>Now let’s compile our first program in the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">HelloWorld.java</span> file using <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac</span> tool. Type the following command to change the current directory to the one where the source file is stored:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">cd C:\Java</span> </strong> </span></p> <p>And type the following command:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac HelloWorld.java</span> </strong> </span></p> <p>That invokes the Java compiler to compile code in the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.java</span> file into bytecode. Note that the file name ends with <span style="color: #800000;">.java</span> extension. You would see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/compile%20HelloWorld%20program.png" alt="compile HelloWorld program" width="239" height="89" /></p> <p>If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.class</span> file which is bytecode form of the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.java</span> file. Now try to type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">dir</span> </strong> </span> in the command line, we’ll see the .class file:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/see%20compiled%20bytecode%20file.png" alt="see compiled bytecode file" width="449" height="170" /></p> <p>So remember a Java program will be compiled into bytecode form (.class file).</p> <p>&nbsp;</p> <h2>5. Run your first Java program</h2> <p>It’s now ready to run our first Java program. Type the following command:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">java HelloWorld</span> </strong> </span></p> <p>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:</p> <p>&nbsp;<img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/run%20hello%20world%20program.png" alt="run hello world program" width="197" height="64" /></p> <p>It just prints out “Hello world!” to the screen and quits. Congratulations! You have successfully run your first Java program!</p> <p>&nbsp;</p> <h2>6. What we have learnt so far</h2> <p>Throughout this tutorial you have learnt the following things:</p> <ul> <li style="list-style-type: none;"> <ul> <li>JDK is the Java SE Development Kit that contains tools and libraries for Java development.</li> <li>JRE is the Java Runtime Environment that enables running Java programs on your computer.</li> <li>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).</li> <li>How to install JDK and configure environment variables.</li> <li><a href="java-core/tools/using-javac-command" target="_blank">javac</a> is the Java compiler. It translates Java source code into bytecode.</li> <li><a href="java-core/tools/examples-of-using-java-command" target="_blank">java</a> is the JVM launcher which we use to run our program.</li> <li>Every Java program starts from the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">main()</span> method.</li> <li>When compiling, the compiler generates a <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> file from a<span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"> <span style="color: #800000;">.java </span></span>file.</li> </ul> </li> </ul> <p>&nbsp;</p> <p><span style="font-size: 13px;">You can also watch the video version of this tutorial:</span></p> <p>https://www.youtube.com/watch?v=-KhuJT-qPwQ</p> <p>Next, I recommend you to read this article:&nbsp;<a href="java-core/the-java-language/java-oop-understand-classes-and-objects" target="_blank">Understand Classes and Objects in Java</a></p> <p>&nbsp;</p> <h3>Related Java Hello World Tutorials:</h3> <ul> <li><a href="java-core/tools/how-to-compile-package-and-run-a-java-program-using-command-line-tools-javac-jar-and-java" target="_blank">How to compile, package and run a Java program using command-line tools (javac, jar and java)</a></li> <li><a href="ides/netbeans/java-hello-world-for-beginner-with-netbeans-ide" target="_blank">Java Hello World for Beginner with NetBeans IDE</a></li> <li><a href="ides/eclipse/how-to-create-build-and-run-a-java-hello-world-program-with-eclipse" target="_blank">How to create, build and run a Java Hello World program with Eclipse</a></li> <li><a href="coding/how-to-compile-and-run-a-java-program-with-textpad" target="_blank">How to Compile and Run a Java Program with TextPad</a></li> <li><a href="coding/how-to-compile-and-run-a-java-program-with-sublime-text-3" target="_blank">How to compile and run a Java program with Sublime Text 3</a></li> </ul></div><div class="feed-description"><p>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.</p> <p>To start, all you need is a fresh computer without any Java software installed, a <a href="coding/how-to-compile-and-run-a-java-program-with-sublime-text-3" target="_blank">text-based editor</a> and a good internet connection.</p> <p><span style="font-size: 12pt;"><strong><span style="line-height: 115%;">NOTES:</span></strong></span><span style="font-size: 14pt; line-height: 115%;"> </span>This beginner tutorial is targeted for Windows environment.</p> <center></center> <p>&nbsp;</p> <h2>1. Download and install Java Development Kit</h2> <p>In order to write and run a Java program, you need to install a software program called <em>Java SE Development Kit</em> (or JDK for short, and SE means Standard Edition). Basically, a JDK contains:</p> <ul> <li style="list-style-type: none;"> <ul> <li><strong>JRE</strong><em>(Java Runtime Environment)</em>: is the core of the Java platform that enables running Java programs on your computer. The JRE includes <strong>JVM&nbsp;</strong><em>(Java Virtual Machine)</em> 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.</li> <li>Tools and libraries that support Java development.</li> </ul> </li> </ul> <p>The JDK ships with two powerful tools which every Java developer should be familiar with:</p> <ul> <li style="list-style-type: none;"> <ul> <li><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">javac.exe</span>: is Java compiler that translates programs written in Java code into bytecode form.</li> <li><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">java.exe</span>: is the Java Virtual Machine launcher that executes bytecode.</li> </ul> </li> </ul> <p>Click <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html" rel="nofollow" target="_blank">this link</a> to download the latest version of JDK installer program:</p> <p>Check the option “<strong>Accept License Agreement</strong>”, and choose an appropriate version for your computer from the list. Here we choose the version for Windows x64:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Download%20JDK.png" alt="Download JDK" width="565" height="464" /></p> <p>After downloading the program, run it to install the JDK on your computer (just following the steps, leave the defaults and click Next, Next…):</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/JDKSetup.png" alt="JDKSetup" width="689" height="543" /></p> <p>You would see the JDK is installed in the following directory, for example: <i>C:\Program Files\Java\jdk1.7.0_21</i>. The following screenshot describes the JDK’s directory structure:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/JDK%20directory%20structure.png" alt="JDK directory structure" width="483" height="352" /></p> <p>Now let’s test if Java runtime is installed correctly. Open a command prompt window and type:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">java -version</span> </strong> </span></p> <p>You would see the following result:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/test%20java%20version.png" alt="test java version" width="508" height="89" /></p> <p>That shows version of the JRE, e.g. “1.7.0_21” - Congratulations! Your computer is now capable of running Java programs.</p> <p>Now try to type the following command:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac -version</span> </strong> </span></p> <p>You would see the following error:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/test%20javac%20version%20error.png" alt="test javac version error" width="494" height="78" /></p> <p>That’s because Windows could not find the <a href="java-core/tools/using-javac-command" target="_blank">javac</a><span style="font-size: 10pt; line-height: 115%;">&nbsp;</span>program, so we need to set some environment variables which tell the location of <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">javac.exe</span>.</span></p> <p>&nbsp;</p> <h2>2. Set up environment variables</h2> <p>Now we’re going to set environment variables so that the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"><span style="color: #800000;">javac.exe</span>&nbsp;</span>program can be accessed anywhere from command line. On Windows 7, go to My Computer and click <strong>System Properties</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/click%20System%20Properties.png" alt="click System Properties" width="233" height="93" /></p> <p>Then click <strong>Advanced system settings</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/click%20Advanced%20system%20settings.png" alt="click Advanced system settings" width="187" height="208" /></p> <p>The <i>System Properties </i>dialog appears, select <i>Advanced</i> tab and click <strong>Environment Variables...</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/System%20Properties%20dialog.png" alt="System Properties dialog" width="426" height="474" /></p> <p>The <i>Environment Variable</i> dialog appears, click on the <strong>New…</strong> button under the <i>System variables</i> section.</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Environment%20Variables%20dialog.png" alt="Environment Variables dialog" width="394" height="436" /></p> <p>That opens up the <i>New System Variable</i> dialog. Type the following information:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/New%20System%20Variable%20dialog.png" alt="New System Variable dialog" width="357" height="153" /></p> <p>The field <i>Variable name</i> must be <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span>, and the field <i>Variable value</i> must point to JDK’s installation directory on your computer. Here it is set to <i>c:\Program Files\Java\jdk1.7.0_21</i>. Click <strong>OK</strong> to close this dialog.</p> <p>Now back to the <i>Environment Variables</i> dialog, look for a variable called <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">Path</span></strong> under the <i>System Variables</i> list, and click <strong>Edit…</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Edit%20Path%20variable.png" alt="Edit Path variable" width="374" height="174" /></p> <p>In the <i>Edit System Variable</i> dialog, append the following to the end of the field <i>Variable value</i>:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">;%JAVA_HOME%\bin</span> </strong> </span></p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/Set%20JAVA_HOME%20in%20Path%20variable.png" alt="Set JAVA HOME in Path variable" width="357" height="153" /></p> <p>Note that there is a semicolon at the beginning to separate this value from other ones. Click <strong>OK</strong> three times to close all the dialogs.</p> <p>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:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac -version</span> </strong> </span></p> <p>You would see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/test%20javac%20version%20success.png" alt="test javac version success" width="163" height="74" /></p> <p>Congratulations! You have completed the setup for essential Java development environment on your computer. It’s now ready to write your first Java program.</p> <p>&nbsp;</p> <h2>3. Code a Java hello world program</h2> <p>Open a simple text editor program such as Notepad and type the following content:</p> <pre class="brush:java;toolbar:false">public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } } </pre> <p>Save the file as <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.java</span><span style="font-size: 10pt; line-height: 115%;"> (note that the extension is </span><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.java</span><span style="font-size: 10pt; line-height: 115%;">) </span>under a directory, let’s say, <i>C:\Java</i>.</p> <p>Don’t worry if you don’t understand everything in this simple Java code. The following picture explains it nicely:</p> <p><img loading="lazy" src="images/articles/javacore/beginner/Hello%20world%20program%20explanation.png" alt="Hello world program explanation" width="695" height="355" /></p> <p>Every Java program starts from the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">main()</span><span style="font-size: 10pt; line-height: 115%;"> </span>method. This program simply prints “Hello world” to screen.</p> <p>&nbsp;</p> <h2>4. Compile your first Java program</h2> <p>Now let’s compile our first program in the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">HelloWorld.java</span> file using <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac</span> tool. Type the following command to change the current directory to the one where the source file is stored:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">cd C:\Java</span> </strong> </span></p> <p>And type the following command:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac HelloWorld.java</span> </strong> </span></p> <p>That invokes the Java compiler to compile code in the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.java</span> file into bytecode. Note that the file name ends with <span style="color: #800000;">.java</span> extension. You would see the following output:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/compile%20HelloWorld%20program.png" alt="compile HelloWorld program" width="239" height="89" /></p> <p>If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.class</span> file which is bytecode form of the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">HelloWorld.java</span> file. Now try to type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">dir</span> </strong> </span> in the command line, we’ll see the .class file:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/see%20compiled%20bytecode%20file.png" alt="see compiled bytecode file" width="449" height="170" /></p> <p>So remember a Java program will be compiled into bytecode form (.class file).</p> <p>&nbsp;</p> <h2>5. Run your first Java program</h2> <p>It’s now ready to run our first Java program. Type the following command:</p> <p style="text-align: center;" align="center"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">java HelloWorld</span> </strong> </span></p> <p>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:</p> <p>&nbsp;<img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/beginner/run%20hello%20world%20program.png" alt="run hello world program" width="197" height="64" /></p> <p>It just prints out “Hello world!” to the screen and quits. Congratulations! You have successfully run your first Java program!</p> <p>&nbsp;</p> <h2>6. What we have learnt so far</h2> <p>Throughout this tutorial you have learnt the following things:</p> <ul> <li style="list-style-type: none;"> <ul> <li>JDK is the Java SE Development Kit that contains tools and libraries for Java development.</li> <li>JRE is the Java Runtime Environment that enables running Java programs on your computer.</li> <li>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).</li> <li>How to install JDK and configure environment variables.</li> <li><a href="java-core/tools/using-javac-command" target="_blank">javac</a> is the Java compiler. It translates Java source code into bytecode.</li> <li><a href="java-core/tools/examples-of-using-java-command" target="_blank">java</a> is the JVM launcher which we use to run our program.</li> <li>Every Java program starts from the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">main()</span> method.</li> <li>When compiling, the compiler generates a <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">.class</span> file from a<span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';"> <span style="color: #800000;">.java </span></span>file.</li> </ul> </li> </ul> <p>&nbsp;</p> <p><span style="font-size: 13px;">You can also watch the video version of this tutorial:</span></p> <p>https://www.youtube.com/watch?v=-KhuJT-qPwQ</p> <p>Next, I recommend you to read this article:&nbsp;<a href="java-core/the-java-language/java-oop-understand-classes-and-objects" target="_blank">Understand Classes and Objects in Java</a></p> <p>&nbsp;</p> <h3>Related Java Hello World Tutorials:</h3> <ul> <li><a href="java-core/tools/how-to-compile-package-and-run-a-java-program-using-command-line-tools-javac-jar-and-java" target="_blank">How to compile, package and run a Java program using command-line tools (javac, jar and java)</a></li> <li><a href="ides/netbeans/java-hello-world-for-beginner-with-netbeans-ide" target="_blank">Java Hello World for Beginner with NetBeans IDE</a></li> <li><a href="ides/eclipse/how-to-create-build-and-run-a-java-hello-world-program-with-eclipse" target="_blank">How to create, build and run a Java Hello World program with Eclipse</a></li> <li><a href="coding/how-to-compile-and-run-a-java-program-with-textpad" target="_blank">How to Compile and Run a Java Program with TextPad</a></li> <li><a href="coding/how-to-compile-and-run-a-java-program-with-sublime-text-3" target="_blank">How to compile and run a Java program with Sublime Text 3</a></li> </ul></div>How to set environment variables for Java using command line2015-07-07T04:02:18-05:002015-07-07T04:02:18-05:00https://codejava.net/java-core/how-to-set-environment-variables-for-java-using-command-lineNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>Why do you need to setup environment variables in Java programming?</p> <p>Well, in <a href="java-core/what-are-jvm-jre-and-jdk" target="_blank">this article</a> 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 (<strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac</span></strong>) and Java launcher (<strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">java</span></strong>) to compile and run Java applications, but these tools don’t know where the JRE or JDK is.</p> <p>So, by setting up the environment variables, you tell your tools that:</p> <p><em>“Hey, you can find the compiler and launcher here and there”.</em>&nbsp;</p> <p>The first thing you need to do after installing the JDK is creating an environment variable named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> and then update the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">PATH</span> variable.</p> <ul> <li><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">JAVA_HOME</span></strong></span>: stores location of the JDK’s installation directory. When you install development tools, they will first check for the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> variable. If found, they will stick with it. If not, they may ask you to manually specify the location where JRE/JDK is installed. <p>&nbsp;</p> </li> <li><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span></strong></span>: 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.</li> </ul> <p>For example, if the JDK is installed at <i>C:\Program Files\Java\jdk1.8.0</i>, then you need to set:</p> <ul> <li><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">JAVA_HOME</span></strong> = C:\Program Files\Java\jdk1.8.0</li> <li><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span></strong> = <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span></strong> + C:\Program Files\Java\jdk1.8.0\bin</li> </ul> <p>Setting these environment variables on Windows is not difficult. Just go <strong>to Control Panel &gt; System &gt; Advanced system settings &gt; Advanced &gt; Environment Variables</strong>. You can see the whole steps to setup this stuff in the following article:</p> <p><strong><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></strong></p> <p>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):</p> <pre class="brush:text">setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx PATH "%PATH%;%JAVA_HOME%\bin";</pre> <p>The <span style="color: #800000;"><strong><span style="font-family: 'Courier New';">setx</span></strong>&nbsp;&nbsp;</span>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 <strong>Start</strong>, type <strong><span style="font-family: 'Courier New';">cmd</span></strong>. When the <strong><span style="font-family: 'Courier New';">cmd.exe</span></strong> icon appears, right click and select <i>Run as administrator</i>.</p> <p>To add/update system environment variables:</p> <pre class="brush:text">setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx -m PATH "%PATH%;%JAVA_HOME%\bin";</pre> <p>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.</p> <p>To summary, here are some important points:</p> <ul> <li>Always set <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME </span>when preparing development environment for Java.</li> <li><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> points to the installation directory of JDK.</li> <li>The <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span> variable should contain an entry pointing to <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME\bin</span>.</li> <li>Environment can be set either via the GUI or command line prompt.</li> </ul> <p>I hope that you have learned something new today. You can also watch the following video for visual steps:</p> <p>https://www.youtube.com/watch?v=nNKMplW-Tmw</p> <p>&nbsp;</p> <h3>Related Tutorials:</h3> <ul> <li><a href="java-core/how-to-set-java-home-environment-variable-on-windows-10" target="_blank">How to set JAVA_HOME environment variable on Windows 10</a></li> <li><a href="java-core/how-to-check-java-version" target="_blank">How to check Java version</a></li> <li><a href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> </ul></div><div class="feed-description"><p>Why do you need to setup environment variables in Java programming?</p> <p>Well, in <a href="java-core/what-are-jvm-jre-and-jdk" target="_blank">this article</a> 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 (<strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javac</span></strong>) and Java launcher (<strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">java</span></strong>) to compile and run Java applications, but these tools don’t know where the JRE or JDK is.</p> <p>So, by setting up the environment variables, you tell your tools that:</p> <p><em>“Hey, you can find the compiler and launcher here and there”.</em>&nbsp;</p> <p>The first thing you need to do after installing the JDK is creating an environment variable named <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> and then update the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">PATH</span> variable.</p> <ul> <li><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">JAVA_HOME</span></strong></span>: stores location of the JDK’s installation directory. When you install development tools, they will first check for the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> variable. If found, they will stick with it. If not, they may ask you to manually specify the location where JRE/JDK is installed. <p>&nbsp;</p> </li> <li><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span></strong></span>: 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.</li> </ul> <p>For example, if the JDK is installed at <i>C:\Program Files\Java\jdk1.8.0</i>, then you need to set:</p> <ul> <li><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">JAVA_HOME</span></strong> = C:\Program Files\Java\jdk1.8.0</li> <li><strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span></strong> = <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span></strong> + C:\Program Files\Java\jdk1.8.0\bin</li> </ul> <p>Setting these environment variables on Windows is not difficult. Just go <strong>to Control Panel &gt; System &gt; Advanced system settings &gt; Advanced &gt; Environment Variables</strong>. You can see the whole steps to setup this stuff in the following article:</p> <p><strong><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></strong></p> <p>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):</p> <pre class="brush:text">setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx PATH "%PATH%;%JAVA_HOME%\bin";</pre> <p>The <span style="color: #800000;"><strong><span style="font-family: 'Courier New';">setx</span></strong>&nbsp;&nbsp;</span>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 <strong>Start</strong>, type <strong><span style="font-family: 'Courier New';">cmd</span></strong>. When the <strong><span style="font-family: 'Courier New';">cmd.exe</span></strong> icon appears, right click and select <i>Run as administrator</i>.</p> <p>To add/update system environment variables:</p> <pre class="brush:text">setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx -m PATH "%PATH%;%JAVA_HOME%\bin";</pre> <p>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.</p> <p>To summary, here are some important points:</p> <ul> <li>Always set <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME </span>when preparing development environment for Java.</li> <li><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> points to the installation directory of JDK.</li> <li>The <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">PATH</span> variable should contain an entry pointing to <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #800000;">JAVA_HOME\bin</span>.</li> <li>Environment can be set either via the GUI or command line prompt.</li> </ul> <p>I hope that you have learned something new today. You can also watch the following video for visual steps:</p> <p>https://www.youtube.com/watch?v=nNKMplW-Tmw</p> <p>&nbsp;</p> <h3>Related Tutorials:</h3> <ul> <li><a href="java-core/how-to-set-java-home-environment-variable-on-windows-10" target="_blank">How to set JAVA_HOME environment variable on Windows 10</a></li> <li><a href="java-core/how-to-check-java-version" target="_blank">How to check Java version</a></li> <li><a href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> </ul></div>How to set JAVA_HOME environment variable on Windows 102019-04-27T02:07:07-05:002019-04-27T02:07:07-05:00https://codejava.net/java-core/how-to-set-java-home-environment-variable-on-windows-10Nam Ha Minhhainatu@gmail.com<div class="feed-description"><p>In this article, you will understand the details about <span style="font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> environment variable and how to set it on Windows 10.&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">What is JAVA_HOME?</span></strong></p> <p>By convention, <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">JAVA_HOME</span></strong></span> 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:</p> <pre class="brush:text">JAVA_HOME = c:\Program Files\Java\jdk1.8.0_201</pre> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">Why is JAVA_HOME needed?</span></strong></p> <p>To develop Java applications, you need to update the <span style="font-family: 'Courier New'; color: #800000;">PATH</span> 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 <span style="font-family: 'Courier New'; color: #800000;">PATH</span> environment variable should include <span style="color: #800000; font-family: 'Courier New';">JAVA_HOME</span>:</p> <pre class="brush:text">PATH = Other Paths + JAVA_HOME</pre> <p>Other paths are set by various programs installed in the operating system. If the&nbsp;<span style="color: #800000; font-family: 'Courier New';">PATH</span> environment variable doesn’t contain a path to JRE/JDK, a Java-based program might not be able to run. For example, typing <span style="font-family: 'Courier New'; color: #800000;">java</span> in the command prompt showing this error:</p> <pre class="brush:text">'java' is not recognized as an internal or external command, operable program or batch file.</pre> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/error_java_command.png" alt="error java command" /></p> <p>Or Eclipse will fail to launch:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Eclipse_launch_error_no_Java_home_found.png" alt="Eclipse launch error no Java home found" /></p> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">How to set JAVA_HOME on Windows 10</span></strong></p> <p>Here are the visual steps to properly set value for the&nbsp;<strong style="color: #800000;"><span style="font-size: 10pt; line-height: 14.2667px; font-family: 'Courier New';">JAVA_HOME</span></strong> and update the&nbsp;<span style="color: #800000; font-family: 'Courier New';">PATH</span> environment variables in order to setup Java development environment on your computer:</p> <p><strong>1.</strong> Firstly, you need to <strong>identify the Java home directory</strong>, which is typically under <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';"><span style="color: #800000;">C:\Program Files\Java</span>&nbsp;</span>directory. Open My Computer and navigate to this directory, you will see:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Java_home_directory.png" alt="Java home directory" width="600" height="262" /></p> <p style="margin-left: 0.25in;">Here, the home of JDK is under <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">C:\Program Files\Java\jdk1.80_201</span>. The version number may vary, depending on the JDK you installed.</p> <p>&nbsp;</p> <p><strong>2.</strong> Open the <strong>System Environment Variables</strong> dialog by typing <i>environment</i> in the search area on Start menu. Click the suggested item <i>Edit the system environment variables</i>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/start_menu_open_system_environment_variable_dialog.png" alt="start menu open system environment variable dialog" /></p> <p style="margin-left: 0.25in;">The <i>System Properties</i> dialog appears, click the button <strong>Environment Variables</strong>.</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/System_properties_dialog.png" alt="System properties dialog" /></p> <p style="margin-left: 0.25in;">Then you will see this dialog:</p> <p style="margin-left: 0.25in;"><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Environment_Variables_dialog.png" alt="Environment Variables dialog" width="580" height="549" /></p> <p>&nbsp;</p> <p><strong>3.</strong> <strong>Create the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> environment variable</strong> by clicking the <strong>New</strong> button at the bottom. In the <i>New System Variable</i> form, enter the name and value as follows:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/New_JAVA_HOME_system_variable.png" alt="New JAVA HOME system variable" /></p> <p>&nbsp;</p> <p>Click OK, and you will see the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> variable is added to the list.</p> <p>&nbsp;</p> <p><strong>4.</strong> <strong>Update the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> system variable</strong>. In the <i>Environment Variables</i> dialog, select the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">Path</span> variable and click <strong>Edit</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Edit_Path_variable.png" alt="Edit Path variable" width="580" height="274" /></p> <p>Then in the <i>Edit environment variable</i> dialog, double click on the empty row just below the last text line, and enter <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">%JAVA_HOME%\bin</span> as follows:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Edit_Path_variable_for_Java_home.png" alt="Edit Path variable for Java home" /></p> <p>The percent signs tell Windows that it refers to a variable – <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span>, and the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">\bin</span> specifies the location of <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">java.exe</span> and <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">javac.exe</span> programs which are used to run and compile Java programs, as well as other tools in the JDK.</p> <p>Click <strong>OK</strong> 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 <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">javac –version</span>, you should see:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/verify_java_home.png" alt="verify java home" /></p> <p><strong><span style="font-size: 12pt; line-height: 107%;">NOTES:</span></strong></p> <p>You can add the path to the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">bin</span> directory of Java home directly into the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> variable. But it’s strongly recommend to follow the above steps to setup a <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">JAVA_HOME</span> variable because many Java programs depend on it.</p> <p>When you installed a JDK, you might not need to update the system variable because the installer already did it for you.</p> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">Learn more:</span></strong></p> <ul> <li><a href="java-core/how-to-set-environment-variables-for-java-using-command-line" target="_blank">How to set environment variables for Java using command line</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li><a href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li><a href="java-core/how-to-check-java-version" target="_blank">How to check Java version</a></li> </ul></div><div class="feed-description"><p>In this article, you will understand the details about <span style="font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> environment variable and how to set it on Windows 10.&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">What is JAVA_HOME?</span></strong></p> <p>By convention, <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">JAVA_HOME</span></strong></span> 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:</p> <pre class="brush:text">JAVA_HOME = c:\Program Files\Java\jdk1.8.0_201</pre> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">Why is JAVA_HOME needed?</span></strong></p> <p>To develop Java applications, you need to update the <span style="font-family: 'Courier New'; color: #800000;">PATH</span> 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 <span style="font-family: 'Courier New'; color: #800000;">PATH</span> environment variable should include <span style="color: #800000; font-family: 'Courier New';">JAVA_HOME</span>:</p> <pre class="brush:text">PATH = Other Paths + JAVA_HOME</pre> <p>Other paths are set by various programs installed in the operating system. If the&nbsp;<span style="color: #800000; font-family: 'Courier New';">PATH</span> environment variable doesn’t contain a path to JRE/JDK, a Java-based program might not be able to run. For example, typing <span style="font-family: 'Courier New'; color: #800000;">java</span> in the command prompt showing this error:</p> <pre class="brush:text">'java' is not recognized as an internal or external command, operable program or batch file.</pre> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/error_java_command.png" alt="error java command" /></p> <p>Or Eclipse will fail to launch:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Eclipse_launch_error_no_Java_home_found.png" alt="Eclipse launch error no Java home found" /></p> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">How to set JAVA_HOME on Windows 10</span></strong></p> <p>Here are the visual steps to properly set value for the&nbsp;<strong style="color: #800000;"><span style="font-size: 10pt; line-height: 14.2667px; font-family: 'Courier New';">JAVA_HOME</span></strong> and update the&nbsp;<span style="color: #800000; font-family: 'Courier New';">PATH</span> environment variables in order to setup Java development environment on your computer:</p> <p><strong>1.</strong> Firstly, you need to <strong>identify the Java home directory</strong>, which is typically under <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';"><span style="color: #800000;">C:\Program Files\Java</span>&nbsp;</span>directory. Open My Computer and navigate to this directory, you will see:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Java_home_directory.png" alt="Java home directory" width="600" height="262" /></p> <p style="margin-left: 0.25in;">Here, the home of JDK is under <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">C:\Program Files\Java\jdk1.80_201</span>. The version number may vary, depending on the JDK you installed.</p> <p>&nbsp;</p> <p><strong>2.</strong> Open the <strong>System Environment Variables</strong> dialog by typing <i>environment</i> in the search area on Start menu. Click the suggested item <i>Edit the system environment variables</i>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/start_menu_open_system_environment_variable_dialog.png" alt="start menu open system environment variable dialog" /></p> <p style="margin-left: 0.25in;">The <i>System Properties</i> dialog appears, click the button <strong>Environment Variables</strong>.</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/System_properties_dialog.png" alt="System properties dialog" /></p> <p style="margin-left: 0.25in;">Then you will see this dialog:</p> <p style="margin-left: 0.25in;"><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Environment_Variables_dialog.png" alt="Environment Variables dialog" width="580" height="549" /></p> <p>&nbsp;</p> <p><strong>3.</strong> <strong>Create the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> environment variable</strong> by clicking the <strong>New</strong> button at the bottom. In the <i>New System Variable</i> form, enter the name and value as follows:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/New_JAVA_HOME_system_variable.png" alt="New JAVA HOME system variable" /></p> <p>&nbsp;</p> <p>Click OK, and you will see the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> variable is added to the list.</p> <p>&nbsp;</p> <p><strong>4.</strong> <strong>Update the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> system variable</strong>. In the <i>Environment Variables</i> dialog, select the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">Path</span> variable and click <strong>Edit</strong>:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Edit_Path_variable.png" alt="Edit Path variable" width="580" height="274" /></p> <p>Then in the <i>Edit environment variable</i> dialog, double click on the empty row just below the last text line, and enter <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">%JAVA_HOME%\bin</span> as follows:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/Edit_Path_variable_for_Java_home.png" alt="Edit Path variable for Java home" /></p> <p>The percent signs tell Windows that it refers to a variable – <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span>, and the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">\bin</span> specifies the location of <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">java.exe</span> and <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">javac.exe</span> programs which are used to run and compile Java programs, as well as other tools in the JDK.</p> <p>Click <strong>OK</strong> 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 <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">javac –version</span>, you should see:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set-java-home/verify_java_home.png" alt="verify java home" /></p> <p><strong><span style="font-size: 12pt; line-height: 107%;">NOTES:</span></strong></p> <p>You can add the path to the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">bin</span> directory of Java home directly into the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> variable. But it’s strongly recommend to follow the above steps to setup a <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">JAVA_HOME</span> variable because many Java programs depend on it.</p> <p>When you installed a JDK, you might not need to update the system variable because the installer already did it for you.</p> <p>&nbsp;</p> <p><strong><span style="font-size: 12pt; line-height: 107%;">Learn more:</span></strong></p> <ul> <li><a href="java-core/how-to-set-environment-variables-for-java-using-command-line" target="_blank">How to set environment variables for Java using command line</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li><a href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li><a href="java-core/how-to-check-java-version" target="_blank">How to check Java version</a></li> </ul></div>What is Java?2012-03-18T22:08:48-05:002012-03-18T22:08:48-05:00https://codejava.net/java-core/what-is-javaNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>Is Java an island of Indonesia archipelago? Yes, that's correct.</p> <p>However, Java is very well known as a software platform, a computer programming language, and a fast growing and widely used technology worldwide.</p> <p>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.</p> <p>&nbsp;</p> <h2>What Java can do:</h2> <p>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.</p> <p>&nbsp;</p> <h2>What makes Java so popular:</h2> <p>Java has a number of advantages which make it a widespreading technology:</p> <ul> <li style="list-style-type: none;"> <ul> <li><em>Platform independent</em>: 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 "<em>Write once, run anywhere</em>".</li> <li><em>Powerful language</em>: 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.</li> <li><em>Secure</em>: Java has a built-in strong security manager from ground-up that prevents malicious code and viruses.</li> <li><em>High performance</em>: with the JVM has been improved over the years, Java applications are fast.</li> <li><em>Networking</em>: with built-in support for networking, developers can write internet-based and networked applications easily.</li> </ul> </li> </ul> <p><strong>&nbsp;</strong></p> <h2>The big family of Java technologies:</h2> <p>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:</p> <ul> <li style="list-style-type: none;"> <ul> <li><strong>Java SE</strong> (Standard Edition): for desktop-based applications.</li> <li><strong>Java EE</strong> (Enterprise Edition): for enterprise web-based applications.</li> <li><strong>Java ME</strong> (Micro Edition): for mobile-based applications.</li> <li><strong>Java FX</strong>: for Rich Internet Applications (RIAs)</li> <li>Java SE for Embedded Devices.</li> </ul> </li> </ul> <p><strong>&nbsp;</strong></p> <h2>The Java logo:</h2> <p>The Java technology is very well known with its logo: a cup of hot coffee.</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/java_logo.jpg" alt="java logo" width="96" height="96" /></p> <p>&nbsp;</p> <h3>Related Tutorials:</h3> <ul> <li><a href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li><a href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li><a href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul></div><div class="feed-description"><p>Is Java an island of Indonesia archipelago? Yes, that's correct.</p> <p>However, Java is very well known as a software platform, a computer programming language, and a fast growing and widely used technology worldwide.</p> <p>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.</p> <p>&nbsp;</p> <h2>What Java can do:</h2> <p>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.</p> <p>&nbsp;</p> <h2>What makes Java so popular:</h2> <p>Java has a number of advantages which make it a widespreading technology:</p> <ul> <li style="list-style-type: none;"> <ul> <li><em>Platform independent</em>: 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 "<em>Write once, run anywhere</em>".</li> <li><em>Powerful language</em>: 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.</li> <li><em>Secure</em>: Java has a built-in strong security manager from ground-up that prevents malicious code and viruses.</li> <li><em>High performance</em>: with the JVM has been improved over the years, Java applications are fast.</li> <li><em>Networking</em>: with built-in support for networking, developers can write internet-based and networked applications easily.</li> </ul> </li> </ul> <p><strong>&nbsp;</strong></p> <h2>The big family of Java technologies:</h2> <p>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:</p> <ul> <li style="list-style-type: none;"> <ul> <li><strong>Java SE</strong> (Standard Edition): for desktop-based applications.</li> <li><strong>Java EE</strong> (Enterprise Edition): for enterprise web-based applications.</li> <li><strong>Java ME</strong> (Micro Edition): for mobile-based applications.</li> <li><strong>Java FX</strong>: for Rich Internet Applications (RIAs)</li> <li>Java SE for Embedded Devices.</li> </ul> </li> </ul> <p><strong>&nbsp;</strong></p> <h2>The Java logo:</h2> <p>The Java technology is very well known with its logo: a cup of hot coffee.</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/java_logo.jpg" alt="java logo" width="96" height="96" /></p> <p>&nbsp;</p> <h3>Related Tutorials:</h3> <ul> <li><a href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li><a href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li><a href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul></div>What are JVM, JRE and JDK?2015-07-07T03:40:21-05:002015-07-07T03:40:21-05:00https://codejava.net/java-core/what-are-jvm-jre-and-jdkNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>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.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">1. What is JVM?</span></h2> <p>JVM stands for <i>Java Virtual Machine</i>. It is an abstract computing machine that is responsible for executing Java programs. When you <a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">write a Java program</a>, 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.</p> <p>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.</p> <p>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.</p> <p>An Oracle’s implementation for JVM is called Java HotSpot VM.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">2. What is JRE?</span></h2> <p>JRE stands for <i>Java Runtime Environment</i>. It provides the libraries, JVM and other components necessary for you to run applets and applications written in the Java programming language.</p> <p>The JRE contains standard tools such as <a href="java-core/tools/examples-of-using-java-command" target="_blank">java</a>, keytool, policytool,… but it doesn’t contain compilers or debuggers for developing applets and applications.</p> <p>When you deploy your Java applications on client’s computer, the client needs a JRE to be installed.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">3. What is JDK?</span></h2> <p>JDK stands for <i>Java Development Kit</i>. It’s a superset of JRE. The JDK includes the JRE plus command-line development tools such as compilers (<a href="java-core/tools/using-javac-command" target="_blank">javac</a>)&nbsp;and debuggers (<strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">jdb</span></strong>) and others (<a href="java-core/tools/using-jar-command-examples" target="_blank">jar</a>, <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javadoc</span></strong>, etc) that are necessary or useful for developing applets and applications.</p> <p>Therefore, as a Java programmer, you have to install JDK as a minimum requirement for the development environment.</p> <p>To summary:</p> <ul> <li><strong>JVM</strong>: is the virtual machine that runs Java applications. The JVM makes Java platform-independence.</li> <li><strong>JRE</strong> = JVM + standard libraries: provides environment for executing Java applications.</li> <li><strong>JDK</strong> = JRE + development tools for compiling and debugging Java applications.</li> </ul> <p>The following picture depicts the relationship among JVM, JRE and JDK:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/jvm/JVM_JRE_JDK.png" alt="JVM JRE JDK" width="400" height="231" /></p> <p>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.</p> <p>And here are some tips:</p> <ul> <li>You should have both JRE and JDK installations (setup) on your computer. You will need both during the development process.</li> <li>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.</li> <li>You should install both 32-bit and 64-bit versions.</li> <li>When installing the JDK, remember to check ‘Install Demos and Samples’. Then you can explore various interesting examples in the <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">demo</span></strong> directory under JDK’s installation path.</li> <li>Only the JDK includes source code of the Java runtime libraries. You can discover the source code in the <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">src.zip</span></strong> file which can be found under JDK’s installation directory.</li> </ul> <p>&nbsp;</p> <h2><span style="line-height: 115%;">4. Where to download JRE and JDK?</span></h2> <p>The starting point is java.oracle.com, but you can go directly to this: <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" rel="nofollow" target="_blank">http://www.oracle.com/technetwork/java/javase/downloads/index.html</a>.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">5. Where to find further information?</span></h2> <p>Here are some official articles from Oracle for your reference:</p> <ul> <li><a href="http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136373.html" rel="nofollow" target="_blank">Java SE HotSpot at a Glance</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/technotes/guides/vm/" rel="nofollow" target="_blank">Java Virtual Machine Technology</a></li> <li><a href="http://docs.oracle.com/cd/E19455-01/806-3461/6jck06gqd/index.html" rel="nofollow" target="_blank">Java Programming Environment and the Java Runtime Environment (JRE)</a></li> </ul> <p>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.</p> <p>&nbsp;</p> <h3 style="padding: 0px; font-family: 'Open Sans', sans-serif; color: #333333;"><span style="margin: 0px; padding: 0px;">Related Topics:</span></h3> <ul style="font-family: Arial, Helvetica, sans-serif; font-size: 14.3px;"> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-se/java-se-versions-history" target="_blank">Java versions history</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul></div><div class="feed-description"><p>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.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">1. What is JVM?</span></h2> <p>JVM stands for <i>Java Virtual Machine</i>. It is an abstract computing machine that is responsible for executing Java programs. When you <a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">write a Java program</a>, 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.</p> <p>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.</p> <p>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.</p> <p>An Oracle’s implementation for JVM is called Java HotSpot VM.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">2. What is JRE?</span></h2> <p>JRE stands for <i>Java Runtime Environment</i>. It provides the libraries, JVM and other components necessary for you to run applets and applications written in the Java programming language.</p> <p>The JRE contains standard tools such as <a href="java-core/tools/examples-of-using-java-command" target="_blank">java</a>, keytool, policytool,… but it doesn’t contain compilers or debuggers for developing applets and applications.</p> <p>When you deploy your Java applications on client’s computer, the client needs a JRE to be installed.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">3. What is JDK?</span></h2> <p>JDK stands for <i>Java Development Kit</i>. It’s a superset of JRE. The JDK includes the JRE plus command-line development tools such as compilers (<a href="java-core/tools/using-javac-command" target="_blank">javac</a>)&nbsp;and debuggers (<strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">jdb</span></strong>) and others (<a href="java-core/tools/using-jar-command-examples" target="_blank">jar</a>, <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">javadoc</span></strong>, etc) that are necessary or useful for developing applets and applications.</p> <p>Therefore, as a Java programmer, you have to install JDK as a minimum requirement for the development environment.</p> <p>To summary:</p> <ul> <li><strong>JVM</strong>: is the virtual machine that runs Java applications. The JVM makes Java platform-independence.</li> <li><strong>JRE</strong> = JVM + standard libraries: provides environment for executing Java applications.</li> <li><strong>JDK</strong> = JRE + development tools for compiling and debugging Java applications.</li> </ul> <p>The following picture depicts the relationship among JVM, JRE and JDK:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/jvm/JVM_JRE_JDK.png" alt="JVM JRE JDK" width="400" height="231" /></p> <p>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.</p> <p>And here are some tips:</p> <ul> <li>You should have both JRE and JDK installations (setup) on your computer. You will need both during the development process.</li> <li>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.</li> <li>You should install both 32-bit and 64-bit versions.</li> <li>When installing the JDK, remember to check ‘Install Demos and Samples’. Then you can explore various interesting examples in the <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">demo</span></strong> directory under JDK’s installation path.</li> <li>Only the JDK includes source code of the Java runtime libraries. You can discover the source code in the <strong><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New';">src.zip</span></strong> file which can be found under JDK’s installation directory.</li> </ul> <p>&nbsp;</p> <h2><span style="line-height: 115%;">4. Where to download JRE and JDK?</span></h2> <p>The starting point is java.oracle.com, but you can go directly to this: <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" rel="nofollow" target="_blank">http://www.oracle.com/technetwork/java/javase/downloads/index.html</a>.</p> <p>&nbsp;</p> <h2><span style="line-height: 115%;">5. Where to find further information?</span></h2> <p>Here are some official articles from Oracle for your reference:</p> <ul> <li><a href="http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136373.html" rel="nofollow" target="_blank">Java SE HotSpot at a Glance</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/technotes/guides/vm/" rel="nofollow" target="_blank">Java Virtual Machine Technology</a></li> <li><a href="http://docs.oracle.com/cd/E19455-01/806-3461/6jck06gqd/index.html" rel="nofollow" target="_blank">Java Programming Environment and the Java Runtime Environment (JRE)</a></li> </ul> <p>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.</p> <p>&nbsp;</p> <h3 style="padding: 0px; font-family: 'Open Sans', sans-serif; color: #333333;"><span style="margin: 0px; padding: 0px;">Related Topics:</span></h3> <ul style="font-family: Arial, Helvetica, sans-serif; font-size: 14.3px;"> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-se/java-se-versions-history" target="_blank">Java versions history</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul></div>How to check Java version2019-04-10T08:32:39-05:002019-04-10T08:32:39-05:00https://codejava.net/java-core/how-to-check-java-versionNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>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).</p> <p>If you have OracleJDK installed on your computer, simply click menu Start, type <strong>about java</strong> into the search area, then click the <strong>About Java</strong> icon in the result. You can see the current version of Java in the following dialog:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/about_java_version.png" alt="about java version" /></p> <p>Or you can go to Control Panel, switch to Large icons view and click the Java logo:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/java_in_control_panel.png" alt="java in control panel" width="500" height="362" /></p> <p>Then in the Java Control Panel dialog, click About button as shown in the following screenshot:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/about_java_control_panel.png" alt="about java control panel" width="480" height="323" /></p> <p>&nbsp;</p> <p>The second way (more familiar with programmers) to check Java version is opening a command prompt and type the following command:</p> <pre class="brush:text">java -version</pre> <p>Suppose that the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> environment variable is updated for Java, you should see the following result:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/check_java_version_console.png" alt="check java version console" /></p> <p>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.</p> <p>Note that “Java(TM) SE Runtime Environment” meaning that you’re using Oracle JDK. In case you’re using OpenJDK, the output should be:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/check_java_version_openjdk.png" alt="check java version openjdk" /></p> <p>&nbsp;</p> <p>If JDK/JRE is not installed or somehow the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> environment variable is not updated for Java, you can see the following message:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/java_is_not_recognized.png" alt="java is not recognized" /></p> <p>&nbsp;</p> <p>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.</p> <p>To check the version exactly, you need to go to the <strong>bin</strong> directory of a JDK/JRE installation, for example:</p> <pre class="brush:text">cd C:\Program Files\Java\jdk1.8.0_201\bin</pre> <p>And type the <strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">java –version</span></strong> command.</p> <p>&nbsp;</p> <p>https://www.youtube.com/watch?v=lwe5gMYugX4</p> <p>&nbsp;</p> <h3>Related Topics:</h3> <ul style="font-family: Arial, Helvetica, sans-serif; font-size: 14.3px;"> <li><a href="java-se/java-se-versions-history" target="_blank">Java versions history</a></li> <li><a href="java-core/how-to-set-environment-variables-for-java-using-command-line" target="_blank">How to set environment variables for Java using command line</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul></div><div class="feed-description"><p>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).</p> <p>If you have OracleJDK installed on your computer, simply click menu Start, type <strong>about java</strong> into the search area, then click the <strong>About Java</strong> icon in the result. You can see the current version of Java in the following dialog:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/about_java_version.png" alt="about java version" /></p> <p>Or you can go to Control Panel, switch to Large icons view and click the Java logo:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/java_in_control_panel.png" alt="java in control panel" width="500" height="362" /></p> <p>Then in the Java Control Panel dialog, click About button as shown in the following screenshot:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/about_java_control_panel.png" alt="about java control panel" width="480" height="323" /></p> <p>&nbsp;</p> <p>The second way (more familiar with programmers) to check Java version is opening a command prompt and type the following command:</p> <pre class="brush:text">java -version</pre> <p>Suppose that the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> environment variable is updated for Java, you should see the following result:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/check_java_version_console.png" alt="check java version console" /></p> <p>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.</p> <p>Note that “Java(TM) SE Runtime Environment” meaning that you’re using Oracle JDK. In case you’re using OpenJDK, the output should be:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/check_java_version_openjdk.png" alt="check java version openjdk" /></p> <p>&nbsp;</p> <p>If JDK/JRE is not installed or somehow the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span> environment variable is not updated for Java, you can see the following message:</p> <p><img loading="lazy" style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/check-version/java_is_not_recognized.png" alt="java is not recognized" /></p> <p>&nbsp;</p> <p>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.</p> <p>To check the version exactly, you need to go to the <strong>bin</strong> directory of a JDK/JRE installation, for example:</p> <pre class="brush:text">cd C:\Program Files\Java\jdk1.8.0_201\bin</pre> <p>And type the <strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">java –version</span></strong> command.</p> <p>&nbsp;</p> <p>https://www.youtube.com/watch?v=lwe5gMYugX4</p> <p>&nbsp;</p> <h3>Related Topics:</h3> <ul style="font-family: Arial, Helvetica, sans-serif; font-size: 14.3px;"> <li><a href="java-se/java-se-versions-history" target="_blank">Java versions history</a></li> <li><a href="java-core/how-to-set-environment-variables-for-java-using-command-line" target="_blank">How to set environment variables for Java using command line</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/features-of-the-java-programming-language" target="_blank">Understand 9 features of Java programming language</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/what-are-jvm-jre-and-jdk" target="_blank">What are JVM, JRE and JDK</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners" target="_blank">How to write, compile and run a hello world Java program for beginners</a></li> <li style="margin: 0px; padding: 0px;"><a style="margin: 0px; padding: 0px; color: #095197; cursor: pointer;" href="java-certifications/how-to-get-java-certifications-5-steps-to-become-a-certified-java-programmer" target="_blank">How to get Java Certificates - 5 Steps to Become a Certified Java Programmer</a></li> </ul></div>How to set JAVA_HOME in macOS and Linux permanently2021-12-30T07:39:17-06:002021-12-30T07:39:17-06:00https://codejava.net/java-core/set-java-home-in-macos-linuxNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>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.</p> <p>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.</p> <p>Basically, you can use the following commands in a terminal for setting JAVA_HOME and updating PATH environment variables:</p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export JAVA_HOME=$HOME/jdk-version/Contents/Home</span></p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export PATH=$JAVA_HOME/bin:$PATH</span></p> <p>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.</p> <p>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.</p> <p>&nbsp;</p> <h2>1. Set JAVA_HOME permanently in Z-shell</h2> <p>Z-shell (zsh) is the default shell in latest macOS operating systems. Firstly, you need to create a Z-shell resource file (<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">.zshrc</span>) in your user home directory, using the following command in terminal:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">cat &gt; .zshrc</span></strong></span></p> <p>Then type the following commands as content of the file:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home</span></strong></span></p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export PATH=$JAVA_HOME/bin:$PATH</span></strong></span></p> <p>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.</p> <p>Press Ctrl + D to save the file. The .<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">zhsrc</span> 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.</p> <p>Now you can type <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">java -version</span> or <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">javac -version</span> to verify. You should see the output looking something like this:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set_java_home_on_macos_zsh.png" alt="set java home on macos zsh" width="800" height="232" /></p> <p>Now, quit the terminal. Then open a new one. Type <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">java -version</span> command again and you should see the change is permanent.</p> <p>&nbsp;</p> <h2><br />2. Set JAVA_HOME permanently in Bash shell</h2> <p>To setup JAVA_HOME with Bash shell, you create the Bash profile file (<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.bash_profile</span> instead of <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.zshrc</span>) in your user home directory, like this:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">cat &gt; .bash_profile</span></strong></span></p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home</span></strong></span></p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export PATH=$JAVA_HOME/bin:$PATH</span></strong></span></p> <p style="text-align: center;"><img src="images/articles/javacore/set_java_home_on_macos_bash.png" alt="set java home on macos bash" width="800" height="92" /></p> <p>Press Ctrl + D to save the file. The <span style="color: #800000;">.<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">bash_profile</span></span> 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.</p> <p>That’s how to set JAVA_HOME environment variable on macOS, with both Bash shell and Z-shell. It is also working on Linux.</p> <p>https://www.youtube.com/watch?v=qotS3IXkJtQ</p> <p>&nbsp;</p> <h3>Related Articles:</h3> <ul> <li><a href="java-core/how-to-set-java-home-environment-variable-on-windows-10">How to set JAVA_HOME environment variable on Windows 10</a></li> <li><a href="java-core/what-are-jvm-jre-and-jdk">What are JVM, JRE and JDK</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners">How to write, compile and run a hello world Java program for beginners</a></li> <li><a href="java-core-tutorials">Java Core Language Tutorials</a></li> </ul></div><div class="feed-description"><p>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.</p> <p>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.</p> <p>Basically, you can use the following commands in a terminal for setting JAVA_HOME and updating PATH environment variables:</p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export JAVA_HOME=$HOME/jdk-version/Contents/Home</span></p> <p style="text-indent: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export PATH=$JAVA_HOME/bin:$PATH</span></p> <p>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.</p> <p>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.</p> <p>&nbsp;</p> <h2>1. Set JAVA_HOME permanently in Z-shell</h2> <p>Z-shell (zsh) is the default shell in latest macOS operating systems. Firstly, you need to create a Z-shell resource file (<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">.zshrc</span>) in your user home directory, using the following command in terminal:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">cat &gt; .zshrc</span></strong></span></p> <p>Then type the following commands as content of the file:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home</span></strong></span></p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export PATH=$JAVA_HOME/bin:$PATH</span></strong></span></p> <p>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.</p> <p>Press Ctrl + D to save the file. The .<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">zhsrc</span> 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.</p> <p>Now you can type <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">java -version</span> or <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">javac -version</span> to verify. You should see the output looking something like this:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javacore/set_java_home_on_macos_zsh.png" alt="set java home on macos zsh" width="800" height="232" /></p> <p>Now, quit the terminal. Then open a new one. Type <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">java -version</span> command again and you should see the change is permanent.</p> <p>&nbsp;</p> <h2><br />2. Set JAVA_HOME permanently in Bash shell</h2> <p>To setup JAVA_HOME with Bash shell, you create the Bash profile file (<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.bash_profile</span> instead of <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.zshrc</span>) in your user home directory, like this:</p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">cat &gt; .bash_profile</span></strong></span></p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home</span></strong></span></p> <p style="text-indent: 0.5in;"><span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">export PATH=$JAVA_HOME/bin:$PATH</span></strong></span></p> <p style="text-align: center;"><img src="images/articles/javacore/set_java_home_on_macos_bash.png" alt="set java home on macos bash" width="800" height="92" /></p> <p>Press Ctrl + D to save the file. The <span style="color: #800000;">.<span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">bash_profile</span></span> 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.</p> <p>That’s how to set JAVA_HOME environment variable on macOS, with both Bash shell and Z-shell. It is also working on Linux.</p> <p>https://www.youtube.com/watch?v=qotS3IXkJtQ</p> <p>&nbsp;</p> <h3>Related Articles:</h3> <ul> <li><a href="java-core/how-to-set-java-home-environment-variable-on-windows-10">How to set JAVA_HOME environment variable on Windows 10</a></li> <li><a href="java-core/what-are-jvm-jre-and-jdk">What are JVM, JRE and JDK</a></li> <li><a href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners">How to write, compile and run a hello world Java program for beginners</a></li> <li><a href="java-core-tutorials">Java Core Language Tutorials</a></li> </ul></div>Download and Install OpenJDK 17 on macOS2022-01-11T02:51:45-06:002022-01-11T02:51:45-06:00https://codejava.net/java-core/install-openjdk-17-on-macosNam Ha Minhhainatu@gmail.com<div class="feed-description"><p>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.</p> <p>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.</p> <p>&nbsp;</p> <h2>1. Download binary distribution of OpenJDK 17 for macOS</h2> <p>Visit the official download page of OpenJDK 17 at <a href="https://jdk.java.net/17" target="_blank">https://jdk.java.net/17</a>, and click on the download link for <strong>macOS/x64</strong> if your computer running on Intel’s CPU, or choose <strong>macOS/AArch64</strong> for Apple’s CPU:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javase/OpenJDK_download_page_for_macOS.png" alt="OpenJDK download page for macOS" width="600" height="503" /></p> <p>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:</p> <p><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #800000;">shasum -a 256 openjdk-xxx.tar.gz</span></span></p> <p>Compare the checksum value printed by the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">shasum</span> command with the value published by the website. If both are identical, you can safely install the archive file.</p> <p>Then extract the downloaded file <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.tar.gz</span> to a directory on your computer, either using context menu provided by macOS (right click on the file, Open with &gt; Archive Utility) or using the following command in a terminal session:</p> <p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">tar -xf openjdk-xxx.tar.gz -C $HOME/OpenJDK</span></p> <p>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 <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">$HOME/OpenJDK/jdk-17.0.1.jdk/Contents/Home</span></p> <h2><br />2. Install OpenJDK 17 on macOS</h2> <p>Next, you need to set <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">JAVA_HOME</span></strong></span> and update <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span></strong></span> 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:</p> <p style="margin-left: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">cat &gt; .zshrc</span></p> <p style="margin-left: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home</span></p> <p style="margin-left: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export PATH=$JAVA_HOME/bin:$PATH</span></p> <p>Press Control + D to save the file. The <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">.zshrc</span> file is run whenever a new terminal session is initialized, which sets <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> and update <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">PATH</span> environment variables.</p> <p><strong>NOTE:</strong> If you’re using Bash shell, create the <span style="color: #800000;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.bash_profil</span>e</span> file instead of <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">.zshrc</span> file.</p> <p>Quit the terminal and start a new session. Then type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">java -version</span></strong></span> command, you should see the following output:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javase/java_version_on_macOS.png" alt="java version on macOS" /></p> <p>That means you have successfully installed OpenJDK on your Mac. To see the steps in action, watch the following video:</p> <p>https://www.youtube.com/watch?v=zwCI3p7Eu8c</p> <p>&nbsp;</p> <h3><span style="font-family: 'Open Sans', sans-serif;">Related Articles:</span></h3> <ul style="font-size: 14.3px;"> <li><a href="java-core/set-java-home-in-macos-linux" target="_blank">How to set JAVA_HOME in macOS and Linux permanently</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core/how-to-set-java-home-environment-variable-on-windows-10">How to set JAVA_HOME environment variable on Windows 10</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core/what-are-jvm-jre-and-jdk">What are JVM, JRE and JDK</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners">How to write, compile and run a hello world Java program for beginners</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core-tutorials">Java Core Language Tutorials</a></li> </ul></div><div class="feed-description"><p>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.</p> <p>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.</p> <p>&nbsp;</p> <h2>1. Download binary distribution of OpenJDK 17 for macOS</h2> <p>Visit the official download page of OpenJDK 17 at <a href="https://jdk.java.net/17" target="_blank">https://jdk.java.net/17</a>, and click on the download link for <strong>macOS/x64</strong> if your computer running on Intel’s CPU, or choose <strong>macOS/AArch64</strong> for Apple’s CPU:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javase/OpenJDK_download_page_for_macOS.png" alt="OpenJDK download page for macOS" width="600" height="503" /></p> <p>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:</p> <p><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #800000;">shasum -a 256 openjdk-xxx.tar.gz</span></span></p> <p>Compare the checksum value printed by the <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">shasum</span> command with the value published by the website. If both are identical, you can safely install the archive file.</p> <p>Then extract the downloaded file <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.tar.gz</span> to a directory on your computer, either using context menu provided by macOS (right click on the file, Open with &gt; Archive Utility) or using the following command in a terminal session:</p> <p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">tar -xf openjdk-xxx.tar.gz -C $HOME/OpenJDK</span></p> <p>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 <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">$HOME/OpenJDK/jdk-17.0.1.jdk/Contents/Home</span></p> <h2><br />2. Install OpenJDK 17 on macOS</h2> <p>Next, you need to set <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">JAVA_HOME</span></strong></span> and update <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">PATH</span></strong></span> 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:</p> <p style="margin-left: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">cat &gt; .zshrc</span></p> <p style="margin-left: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home</span></p> <p style="margin-left: 0.5in;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">export PATH=$JAVA_HOME/bin:$PATH</span></p> <p>Press Control + D to save the file. The <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">.zshrc</span> file is run whenever a new terminal session is initialized, which sets <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">JAVA_HOME</span> and update <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">PATH</span> environment variables.</p> <p><strong>NOTE:</strong> If you’re using Bash shell, create the <span style="color: #800000;"><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">.bash_profil</span>e</span> file instead of <span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New'; color: #800000;">.zshrc</span> file.</p> <p>Quit the terminal and start a new session. Then type <span style="color: #800000;"><strong><span style="font-size: 10pt; line-height: 107%; font-family: 'Courier New';">java -version</span></strong></span> command, you should see the following output:</p> <p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/articles/javase/java_version_on_macOS.png" alt="java version on macOS" /></p> <p>That means you have successfully installed OpenJDK on your Mac. To see the steps in action, watch the following video:</p> <p>https://www.youtube.com/watch?v=zwCI3p7Eu8c</p> <p>&nbsp;</p> <h3><span style="font-family: 'Open Sans', sans-serif;">Related Articles:</span></h3> <ul style="font-size: 14.3px;"> <li><a href="java-core/set-java-home-in-macos-linux" target="_blank">How to set JAVA_HOME in macOS and Linux permanently</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core/how-to-set-java-home-environment-variable-on-windows-10">How to set JAVA_HOME environment variable on Windows 10</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core/what-are-jvm-jre-and-jdk">What are JVM, JRE and JDK</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners">How to write, compile and run a hello world Java program for beginners</a></li> <li><a style="color: #095197; cursor: pointer;" href="java-core-tutorials">Java Core Language Tutorials</a></li> </ul></div>