×

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
How to Run Java Program in Ubuntu

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:

How to Run Java Program in Ubuntu

Here we create a notepad file which is saved as Demo1.

Over the notepad, we can write the java program as shown below:

How to Run Java Program in Ubuntu

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.

How to Run Java Program in Ubuntu

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.


Related Topics

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.