Java for beginners: Installation of Java

smartjeff

Jeff Odhiambo

Posted on March 5, 2022

Java for beginners: Installation of Java

How do you know that you have java installed? How do you install Java? How do you write, compile and run a java code?
We will perform all these in this article which will be a continuation of our previous article on Java, Introduction to Java.

Check installation

To run Java programs on your computer you must at least have the Java runtime environment (JRE) installed. This might already be the case on your machine. You can test if the JRE is installed and in your current path by opening a terminal or cmd in windows(Win+R, enter cmd and press Enter) and by typing in the following command:

java -version
Enter fullscreen mode Exit fullscreen mode

If the JRE is installed and within your path, this command will print information about your Java installation as shown below.

C:\Users\prof>java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
Enter fullscreen mode Exit fullscreen mode

If the command line returns the information that the program could not be found, you have to install Java.

How to install java on Ubuntu or any UNIX system

On Ubuntu, you can install the latest Java through the terminal by writing the following command.

sudo apt-get install openjdk
Enter fullscreen mode Exit fullscreen mode

or you can specify the version, e.g for Java 7 we can use

sudo apt-get install openjdk-7-jdk
Enter fullscreen mode Exit fullscreen mode

How to install Java on Windows

For Windows, Oracle provides a native installer which can be found on the Oracle website. The central website for installing Java is located under the following URL which also contains instructions on how to install
Java for other platforms. java.com

What to do if you come across an installation problem

If you have problems installing Java on your system, search via Google for How to install JDK on <YOUR_OS>. This should result in helpful links. Replace <YOUR_OS> with your operating system, e.g., Windows, Ubuntu, Mac OSX, etc.
For instance, if having problem in installing Java on windows
Search How to install java on windows which will result to results.

Validate installation

After installation, you can confirm if Java is correctly installed by running java -version on your terminal or cmd.
If successful the output will be similar to

C:\Users\prof>java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
Enter fullscreen mode Exit fullscreen mode

How can you tell you are using a 32-bit or 64-bit version of Java?

You can run a 32b-it or a 64-bit version of Java on a 64-bit system. If you use java -version and the output contains the "64-Bit" string, then you are using the 64-bit version of Java otherwise you are using the 32-bit version.
The following is the output of a 64-bit version.

C:\Users\prof>java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
Enter fullscreen mode Exit fullscreen mode

Writing, compiling, and running a Java program

Write source code

To create Java programs, use any text editor such as Notepad, WordPad, or gedit in ubux/Linux. After creation, save the file with .Java file extension. Example: Create file HelloWorld.java then write the following source code:

// Simplest Java program
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
Enter fullscreen mode Exit fullscreen mode

Congratulations, you just created your first java source file.
The above Java program can be developed under Linux/Windows using a text editor and the command line. Select or create a new directory in Windows which will be used for your Java development. In this description, the path C:\Users\prof\Desktop\javalessons is used.
On Linux, you might see \home\<name>\javalessons but this will depend on how you named your folder and the location of the folder.
We will ignore the first file you created.
Once done creating a directory, open a text editor which supports plain text, e.g., gedit in Linux or
Notepad in Windows and write the following source code.

// Simplest Java program
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World, Thanks for reading this article. Did you find it useful");
    }
}
Enter fullscreen mode Exit fullscreen mode

Save the source code in your javadir directory(my case it's, C:\Users\prof\Desktop\javalessons) with the HelloWorld.java filename. The name of a Java source file must always be the same as the class name(within the source code) and end with the .java extension. In this example, the filename must be HelloWorld.java, because the class is called HelloWorld.

Compiling and Running Java Programs

Open a terminal or command line. Switch to the javadir directory using the command cd javadir, e.g, in the above example I'll use cd C:\Users\prof\Desktop\javalessons command to change directory to where the file is located.
Then use the dir command (ls in Linux) to verify that the source file is in the directory.

C:\Users\prof>cd C:\Users\prof\Desktop\javalessons

C:\Users\prof\Desktop\javalessons>dir

05/03/2022  14:54    <DIR>          .
05/03/2022  14:54    <DIR>          ..
05/03/2022  14:54               151 HelloWorld.java
               1 File(s)            151 bytes

C:\Users\prof\Desktop\javalessons>
Enter fullscreen mode Exit fullscreen mode

To compile the source code file into a class file, use the javac command. For example, the following command compiles HelloWorld.java:

javac HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

Afterward, list again the content of the directory with the ls or dir command. The directory contains now a file HelloWorld.class. If you see this file, you have successfully compiled your first Java source code into bytecode.

C:\Users\prof\Desktop\javalessons>javac HelloWorld.java

C:\Users\prof\Desktop\javalessons>dir

05/03/2022  15:04    <DIR>          .
05/03/2022  15:04    <DIR>          ..
05/03/2022  15:04               425 HelloWorld.class
05/03/2022  14:54               151 HelloWorld.java
               2 File(s)            576 bytes

C:\Users\prof\Desktop\javalessons>
Enter fullscreen mode Exit fullscreen mode

You can now start to run your compiled Java program. Ensure that you are still in the jardir directory.
To run, use the java command. For example, the command java HelloWorld will display Hello World, Thanks for reading this article. Did you find it useful?.

C:\Users\prof\Desktop\javalessons>java HelloWorld
Hello World, Thanks for reading this article. Did you find it useful?

C:\Users\prof\Desktop\javalessons>
Enter fullscreen mode Exit fullscreen mode

Using the classpath

Common Error when compiling programs is:

Error
To solve the problem, set the classpath in the environmental variables as follows:

Classpath

The classpath defines where the Java compiler and Java runtime look for .class files to load. These instructions can be used in the Java program. You can use the classpath to run the program from another place in your directory.

Configure classpath in Java environment on Windows (Windows10)

  1. Click on the Start menu
  2. Search Advanced System setting Advance system setting
  3. Click on Environment Variables, under System Variables, find path, and click on it. system variables Path
  4. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value .e.g PATH= C:\ProgramFiles\Java\jdk1.7.0_45\bin. In my case, it is already configured.
  5. Reopen Command prompt window, and run your java code using the javac command.

Congratulations you completed the second article on Java. If you missed the first one, kindly visit Introduction to Java.

Next article is Java Fundamentals

💖 💪 🙅 🚩
smartjeff
Jeff Odhiambo

Posted on March 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related