Tech
Infected

Linux, OpenSource, Programming And Hacks

Apr 1, 2018

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

Apr 1, 2018

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

don't put the .class extension just the class name

Done!



No comments:

Post a Comment