How to compile and run Java program in Linux / Ubuntu Terminal

Writing a Java program is quite straight forward in Linux systems, no matter what linux distro (Linux Mint / Ubuntu / Debian / Arch Linux) you have, the steps will same except for step 1 which is installation, it may differ distro to distro

Here we will write, compile and execute a simple Hello World Program.

So, the first step is to install Java on your computer.

1. Install Java software development kit.

sudo apt-get install openjdk-8-jdk

2. write your program

you can write your program using any text editor. in the terminal you can use VIM or nano editor.

nano HelloWorld.java

Note: Class name is same as the filename, HelloWorld

3. Now, compile your program

javac HelloWorld.java

HelloWorld.class, a bytecode file will get generated which we will execute in the next step.

4. Finally, run your program.

java HelloWorld

notice that here we don’t put the .class extension but just the class name

Done!

Leave a Reply

Your email address will not be published.