Category: Java

Added: 16th of October 2021

Viewed: 170 times

How to compile your Java code using the terminal in Linux, Learning Java

After trying a bit of programming in C++, I have now decided to turn my attention to learning a bit of Java.

Doing just some very basic programming in C++ has helped me get a better understanding of Java.

If you want to start programming in Java on Ubuntu and / or other Ubuntu based distribution you first need to install the correct java packages, these are:

JDK - Java Development Kit
JRE - Java Runtime Environment

To install these packges in your distribution enter the following commands

sudo apt-get install default.jdk

sudo apt-get install default-jre


Once installed enter the following command to check each installed version
java --version


Output
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.10)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.10, mixed mode, sharing)


javac --version


Output
javac 11.0.11


After that there are a variety of IDE's you can use to start writing Java Code. Some of these include Eclipse, Microsoft Visual Code Editor

I currently use Bluej which is ideal for learning Java. I can also compile and execute the code inside Bluej, but to begin with you can write some java code using a text editor in your linux distribution. Once saved you can then compile and execute your code using the terminal.

Let's create a program in Java that prints Hello Computer to the console window.

Create a new file named HelloComputer.java on your desktop.

Next copy and paste the code below and save the file
public class HelloComputer {

public static void main (String[] args) {

System.out.println("Hello Computer");
}

}


In order to compile your code manually enter the following command
javac HelloComputer.java


If there a no errors in your code, then a .class file will be generated, ie HelloComputer.class



To execute your java enter the following command
java HelloComputer


Output


What I have also learnt is that you should save your .java source file with the same name as the Class Name in your code, this will make it easier to identify your code and also avoid errors when compiling.

Other Tips & Tutorials

Java

Install Bluej on Ubuntu, Java Development Environment for beginners

Posted: 19th of November 2021

Views: 127

Java

Simple for loop in Java. Print the numbers 1 to 10 in the console

Posted: 13th of November 2021

Views: 219

Java

Reading user input and outputting the results to console in Java using Scanner class

Posted: 27th of October 2021

Views: 151

Java

Printing to console in Java using the System.out.println() command, learning Java

Posted: 19th of October 2021

Views: 111

Java

How to compile your Java code using the terminal in Linux, Learning Java

Posted: 16th of October 2021

Views: 170