So, you have started learning Java programming? That’s good.
And you want to run the java programs on your Linux system? Even better.
Let me show how to run Java in terminal in Ubuntu and other Linux distributions.
Running Java programs in Ubuntu
Let’s go in proper steps here.
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.
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.
This is why I advise installing Eclipse on Ubuntu for proper Java programming. It is easier to program in an IDE.
I hope you find this tutorial helpful. Questions or suggestions? The comment section is all yours.