Java is the most popular programming language among programmers. Also, Java is a platform independent Object Oriented Programming language. It has the same features as other languages in the programming world. I assume that you have the basic Java programming knowledge and you’ve already compiled java codes on the windows machines and now you’re new to Ubuntu! So, Let’s get started with today’s article!Java has a compiler like other languages. With the help of Compiler, We can covert our java program into a special kind of file called "java byte code". But we can't run Java byte code directly in over machine.
Therefore java needs a special kind of program for convert java code into machine language. Its called "(JVM) Java Virtual Machine". In this article, I would like to explain, How to install Java on Ubuntu 19.04 LTS.
I'll start my instruction on how to install java(JDK - Java Development Kit) from default Ubuntu repositories using the APT tool. JRE(Java Runtime Environment) is by-default installing with JDK Package.
Install Java on Ubuntu
Update your Ubuntu Machine from following command.
sudo apt-get update && apt-get upgrade
Install Default JDK Package from APT Tool. Enter the following command and hit Enter.
sudo apt-get install default-jdk
That's all the steps for the Java installation part on Ubuntu. The APT tool will install Java 10 or 11 version. Now time to move the java configuration part.
Configure Java on Ubuntu
Before configuring the Java, check whether it correctly installed or not. To check it, Run the following command and check the java version.
The Output of the above command should be like this:
Set the JAVA_HOME variable on Ubuntu
It's important to setup JAVA_HOME variable because other applications will able to find your java installation path from this step. To find it run the following command.
update-alternatives --config java
The output of the above command should be like this:
I highlighted the path in above screen shot from my Ubuntu Machine. Then copy that path.
Now open the "/etc/environment" file from the nano text editor On Ubuntu. nano is a very simple text editor. We're going to edit the environment file, coz open it with root permissions. Use the sudo command to gain admin privileges.
sudo nano /etc/environment
Add the following code like to end of the environment file.
Replace with your Java installation Path from clipboard and press CTRL + S to save the file and CTRL + X to close the file. Now reload the environment file from the following command.
Now try to echo JAVA_HOME variable from terminal. Type the following.
The output of the about command is installation path of your java jdk.
That's all for Java Installing and setting Java path on Ubuntu. Now you can download code editor like visual studio code or use the terminal to write java codes.
How to Compile Java code From Terminal?
It's a very easy task! Save your java code with a .java extension. (file name should be equal to the main class name of your code). Suppose you have saved the file named "Hello.java"
Now navigate the save file location and type the following command to compile it:
javac means Java Compiler.
Now Java compiler creates a byte code from your original java file. Now it can easily run on JDK. Run the following to execute it:
and that's it!
If you have any concerns about this quick article, the comment section is always open for you! Also, your valuable suggestions for improving the Ubuntupoint are most welcome.