Hello there
I am back with another article about Java. In the previous article we learned how to install Java JDK in Arch Linux. Today we will learn to compile and run Java program in Linux. You must have JDK installed before you can compile and run Java program. If you do not have JDK installed, install it first.
Our Java Program
public class HelloWorld{
public static void main(String [] args) {
System.out.println("Hello ! World");
}
}
This is a simple Hello World program. This program will print “Hello ! World” in terminal. So let’s see how we can compile it.
To compile a Java program we will use the Javac Compiler which comes with Java JDK. We will run the following command to compile our Java program.
javac HelloWorld.java
Here HelloWorld.java is the name of our file. Remember, the file name must be same as class name. After running the above command, Javac will create a class file with the file name (HelloWorld). In our case it will be HelloWorld.class
Now we need to run our Java program. For this we will run the following command
java HelloWorld
Notice here, we didn’t put the .class extension while we were running the program. We just used the class name.
The program will output something like below
As you can see, it is very easy to compile and run the Java program on Linux. I hope you enjoyed this article and don’t forget to share your thoughts in the comments section.