Java Getting Started


Java Install

Some PCs may already have Java installed.

To check if Java is installed on Windows PC, search the first Java bar or type in the following Command Prompt (cmd.exe):


C:\Users\Your Name>java -version

When Java is installed, you will see something similar (depending on version):


java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

If you do not have Java installed on your computer, you can download it for free from oracle.com.

Note: In this tutorial, we will write the Java code in the text editor. However, you may be able to write Java in the Integrated Development Area, such as IntelliJ IDEA, Netbeans or Eclipse, which are very useful when managing large Java file collections.



Setup for Windows

To install Java on Windows:

  1. Go to "System Properties" (Available in Control Panel> System & Security> System> Advanced System Settings)
  2. Click the "Natural Variables" button under the "Advanced" tab. Then, select the "Method" variable in the System variable and click the "Edit" button.
  3. Click the "New" button and add the path where Java is installed, followed by \ bin. By default, Java is included in C: \ Program Files \ Java \ jdk-11.0.1 (If nothing else is specified when you install it). If so, You will need to add a new path: C: \ Program Files \ Java \ jdk-11.0.1 \ bin
  4. Then, click "OK", and save the settings
  5. Finally, open Command Prompt (cmd.exe) and type a java -version to see if Java is running on your machine.


Java Quickstart

In Java, every application starts with a class name, and that class must match the file name.

Let's create our first Java file, called Main.java, which can be created in any text editor (like Notepad).

The file should contain the message "Hello World", encoded with the following code:


Main.java
public class Main {
            public static void main(String[] args) {
              System.out.println("Hello World");
            }
          }
          

Do not worry if you do not understand the code above - we will discuss it in more detail in future chapters. In the meantime, focus on using the code above.

Save the code to Notepad as "Main.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and then type "javac Main.java":


C:\Users\Your Name>javac Main.java

This will include your code. If there are no errors in the code, the command information will take you to the next line. Now, type "java Main" to use file:


C:\Users\Your Name>java Main

Output should read:


Hello World

Congratulations! You have written and executed your first Java application.