How to run java program in ubuntu
To run the java programming in ubuntu, we need to follow several steps:
Let's see the process.
Step1: Install the Java compiler or JDK to the system.
To run the java program, we need to check whether Java is installed or not?
If it is installed, you have to compile the java program.
As we all know, the java compiler is a part of JDK (Java Development Kit). You must install JDK to compile or run the java program in ubuntu.
For the installation purpose, you can take the support of web browsers like google chrome.
After successfully installing JDK into your system, you have to check the current version of the JDK and whether Java is installed or not?
The command for check java is installed or not
javac - -version
When you see the error as Java not found, it means the JDK (java development tool kit) hasn't been installed yet.
For installing the Java in Ubuntu, we have to type the command as
Sudo apt install default-JDK
First, we must install the ubuntu terminal to run this command. Then type the following command in the prompt.
After installing the system will be asked for the password. After entering the password, the screen doesn't show anything after pressing the enter key.
The following command will work on the ubuntu based software's Linux, mint, and based operating systems. For another Operating system, we have to install a separate package manager. The package name would also be different.
After installing, we have to check the version.
Step2: Compiling the java program in the ubuntu
Let's create a new program for Java.
As we know that Java follows a particular structure.
- Documentation section
- Package section
- Global declaration section
- Interface section
- Class section
- Class with the main section
In java class main must be the program saving name like,
Demo.java and class name must be in camel case letters.
Now let's see the simple program
// program for hello world in java // Document section
Import java.io.*; // package section
Class Demo
{
Public static void main(String args[]) // class with main
{
System.out.println(“Hello World!!”);
}
}
This program does not use global declaration, interface, or class sections.
For compiling, we have to follow the command as
Javac Demo.java
After compiling the command, if the system doesn't produce any error, the following command doesn't produce any output. It generates the class file as a .class file with the name used in the program for saving. Later we have to run and execute the program.
Step 3: To run the java file.
Here we have to use the command as Java. For compilation only, we use the javac.
For execution or running the program, we have to follow the command as
Java Demo.java
After compiling the program, the system will produce the following output.
Output:
Hello World!!
This is the following output for the following program. This is the simplest program for running the java program in ubuntu.
After installing the JDK (java development kit), the compiling process will be easy and simple.
Let's see another program for running the java programming in the normal command prompt. To see the difference between the ubuntu and the command prompt terminal.
Firstly, we have to open the command prompt and check the java version in the c drive and check the version using the following command as
Java - -version

After checking, the version creates a directory in the c drive: as mkdir java.
After creating the directory, create the java file over there. The following command for creating a java file in notepad:
notepad Demo1.java
Demo1 is the file extension name to compile and save the program. Then it will create an interface as:

Here we create a notepad file which is saved as Demo1.
Over the notepad, we can write the java program as shown below:

This is the sample java program for adding the two numbers. After saving the java program, we run the following command in the java program as
javac Demo1.java.
After successful compilation, if there is no error, it goes to the execution state.

After successful execution, it provides the following output:
Output:
a = 3
b = 5
(a + b) = 8
Hence the following differences between the java program in ubuntu and the normal Windows terminal.