How to Run Java Programs in Ubuntu

How to Run Java Programs in Ubuntu

Today in this tutorial we are going to learn How to Run Java Programs in Ubuntu step bye step, Java is a most popular high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. Well that’s good, you stare3d to learn java programming.

And you want to run the java programs on your Linux system? So let’s get start how to run Java in terminal in Ubuntu and other Linux distributions.

Install Java and run Java programs in Ubuntu

Step 1: Install Java compiler

To run a Java program, you need to compile the program first. You need Java compiler for this purpose.

The Java compiler is part of JDK (Java Development Kit). You need to install JDK in order to compile and run Java programs.

First, check if you already have Java Compiler installed on your system:

javac --version

If you see an error like “Command ‘javac’ not found, but can be installed with”, this means you need to install Java Development Kit.

The simplest way to install JDK on Ubuntu is to go with the default offering from Ubuntu:

sudo apt install default-jdk

You’ll be asked to enter your account’s password. When you type the password, nothing is seen on the screen. That is normal. Just enter your password blindly. When asked, press the enter key or Y key.

The above command should work for other Debian and Ubuntu based distributions like Linux Mint, elementary OS etc. For other distributions, use your distribution’s package manager. The package name could also be different.

Once installed, verify that javac is available now.

Step 2: Compile Java program in Linux

You need to have a Java program file for this reason. Let’s say you create a new Java program file named HelloWorld.java and it has the following content:

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

You can use Nano editor in terminal or Gedit graphical text editor for writing your Java programs.

javac HelloWorld.java

If there is no error, the above command produces no output.

When you compile the Java program, it generates a .class file with the class name you used in your program. You have to run this class file.

Top 5 Linux Themes for Mate Desktop Environment

Step 3: Run the Java class file

You do not need to specify the class extension here. Just the name of the class. And this time, you use the command java, not javac.

java HelloWorld

This will print Hello World on the screen for my program.

And that’s how you run a Java program in the Linux terminal.

This was the simplest of the example. The sample program had just one class. The Java compiler creates a class file for each class in your program. Things get complicated for bigger programs and projects.

I hope you find this tutorial helpful. Questions or suggestions? The comment section is all yours.

Be the first to comment

Leave a Reply

Your email address will not be published.


*