×

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

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

JDBC Architecture

JDBC: JDBC stands for Java Database Connectivity. Sun Microsystems has a specification called JDBC. JDBC is a Java API (Application Programming Interface) that enables users to interact or communicate with...

4 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that...

2 minutes read.

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...

3 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.