To run java program in ubuntu

How to Run Java Files in Ubuntu Terminal: Step-by-Step Guide

Learn how to run Java files in Ubuntu terminal with our step-by-step guide. Install JDK, compile Java programs, run Java programs, and set JAVA_HOME variable.

  • Installing the JDK on Ubuntu
  • Compiling Java Programs
  • How to Run Java Program in Terminal Ubuntu Linux
  • Running Java Programs
  • Setting the JAVA_HOME Variable
  • Best Practices for Java Development
  • Other quick code examples for running Java files in Ubuntu terminal
  • Conclusion
  • How do I run a Java file in Ubuntu terminal?
  • How to run Java file on terminal?
  • How to run Java on Linux terminal?
  • Can Java run on Ubuntu?

Running Java programs in Ubuntu and other Linux operating systems is a crucial aspect of Java development. The Java Development Kit (JDK) needs to be installed to compile and run Java programs. In this blog post, we will guide you on how to run Java files in Ubuntu terminal. We will provide you with step-by-step guidance, real-life examples, and case studies to illustrate how it has been successfully used in various fields.

Читайте также:  Resize all html elements

Installing the JDK on Ubuntu

Before we can compile and run Java programs in Ubuntu, we need to install the Java Development Kit (JDK). There are two ways to install the JDK on Ubuntu: using apt-get or downloading the package from the official website.

Installing the JDK using apt-get

The easiest way to install the JDK on Ubuntu is by using apt-get. Open your terminal and type the following command:

sudo apt-get install openjdk-8-jdk 

This command will install the latest version of the JDK available in Ubuntu’s repositories. Once the installation is complete, you can verify that the JDK has been installed by typing the following command:

If the JDK is installed correctly, you should see the version of the JDK installed on your system.

Downloading the JDK from the Official Website

If you prefer to download and install the JDK from the official website, you can follow these steps:

  1. Go to the following link: https://www.oracle.com/java/technologies/javase-jdk8-downloads.html
  2. Click on the “Download” button under “JDK”.
  3. Accept the license agreement and select the appropriate package for your system.
  4. Save the file to your computer and open your terminal.
  5. Navigate to the directory where you saved the JDK package and type the following command:
sudo tar xvzf jdk-8u281-linux-x64.tar.gz -C /opt/ 
  1. Replace jdk-8u281-linux-x64.tar.gz with the name of the file you downloaded.
  2. Once the command is complete, you can verify that the JDK has been installed by typing the following command:
/opt/jdk1.8.0_281/bin/javac -version 

If the JDK is installed correctly, you should see the version of the JDK installed on your system.

Читайте также:  Set image name html

Compiling Java Programs

Once the JDK is installed on your system, you can start Compiling Java Programs. There are several ways to compile Java programs in Ubuntu, but we will cover two of the most common methods: using the “javac” command and using IntelliJ IDE.

Using the “javac” command

The “javac” command is a command-line tool that comes with the JDK and is used to compile Java programs. To compile a Java program using the “javac” command, follow these steps:

  1. Open your terminal and navigate to the directory where your Java program is located.
  2. Type the following command to compile your Java program:

Replace with the name of your Java file. If the compilation is successful, you should see a new file with a “.class” extension in the same directory.

Using IntelliJ IDE

IntelliJ IDEA is a popular Java Integrated Development Environment (IDE) that provides a graphical user interface for Java development. To compile a Java program using IntelliJ IDEA, follow these steps:

  1. Open IntelliJ IDEA and create a new Java project.
  2. Create a new Java class and write your Java program in the code editor.
  3. Click on the “Build” menu and select “Build Project” to compile your Java program.
  4. If the compilation is successful, you should see a new file with a “.class” extension in the “out/production” directory of your project.

How to Run Java Program in Terminal Ubuntu Linux

In this video I am going to show you How to install Java JDK 10 on Ubuntu Linux ( with Duration: 11:41

Running Java Programs

Once your Java program is compiled, you can run it in Ubuntu using the Java Virtual Machine (JVM). To run a Java program in Ubuntu, follow these steps:

  1. Open your terminal and navigate to the directory where your Java program is located.
  2. Type the following command to run your Java program:

Replace with the name of your Java file (without the “.java” extension). If the program runs successfully, you should see the output in your terminal.

Setting the JAVA_HOME Variable

The JAVA_HOME variable is an environment variable that points to the location of the JDK on your system. Setting the JAVA_HOME variable is not required to compile and run Java programs in Ubuntu, but it can be useful if you are using third-party tools that require the JDK.

Setting the JAVA_HOME Variable Globally

To set the JAVA_HOME variable globally for all users on your Ubuntu system, follow these steps:

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" 

Replace /usr/lib/jvm/java-8-openjdk-amd64 with the path to your JDK installation.

  1. Save the file and exit the text editor.
  2. Reload the environment variables by typing the following command:

Advantages of Setting the JAVA_HOME Variable

Setting the JAVA_HOME variable can be useful if you are using third-party tools that require the JDK. By setting the JAVA_HOME variable, you can ensure that the tools are using the correct version of the JDK and that you are not encountering any compatibility issues.

Best Practices for Java Development

Java is a powerful and versatile programming language that is used in a wide range of applications. To get the most out of Java development, it is important to follow best practices and coding conventions.

Following Coding Conventions

Following coding conventions is essential for writing maintainable and readable code. Some of the coding conventions for Java include:

  • Using camelCase for variable and method names.
  • Using PascalCase for class names.
  • Indenting code with four spaces.
  • Using braces to denote code blocks, even when the block only contains a single statement.

Modularizing Code

Modularizing code is essential for writing scalable and maintainable applications. By breaking your code into smaller, more manageable modules, you can improve code reuse and make your code easier to understand.

Testing Thoroughly

Thoroughly testing your Java code is essential for ensuring that your application is stable and bug-free. There are several testing frameworks available for Java, including JUnit and TestNG.

Advantages and Disadvantages of Java

Java has several advantages over other programming languages, including:

  • Platform independence: Java code can run on any platform that supports the JVM.
  • Object-oriented: Java is a fully object-oriented language, making it easy to write modular and reusable code.
  • Large community: Java has a large and active community, making it easy to find resources and support.

However, Java also has some disadvantages, including:

  • Slower performance: Java code can be slower than code written in other languages like C++.
  • Limited access to hardware: Java’s platform independence can limit its access to hardware resources.

Using Open-Source Tools and Frameworks

There are several open-source tools and frameworks available for Java development, including Spring, Hibernate, and Apache Struts. By using open-source tools and frameworks, you can save time and improve the quality of your code.

Other quick code examples for running Java files in Ubuntu terminal

In Java , for example, run java from terminal linux code example

//To compile, type javac filename.java//To run the generated class file, use java filename

Conclusion

Running Java Programs in Ubuntu is an essential aspect of Java development. In this blog post, we have provided you with a step-by-step guide on how to run Java files in Ubuntu terminal. By following the steps outlined in this post, you can compile and run Java programs on Ubuntu successfully. Additionally, we have discussed best practices for java development and provided you with helpful tips and insights to improve your Java programming skills.

Источник

Your First Java Program in the Ubuntu Terminal

Ubuntu Java Development

Ubuntu Java Development

If you are new to Java Programming in the Ubuntu operating system, this simple tutorial will guide you along in writing and compiling your first Java Hello World program. The applications that you need for this purpose include the Java Runtime Environment and the Java Development Kit. This article covers the installation of these two through the Ubuntu command line. You can then write your first program in a text editor such as gedit, nano or even some graphical text writing tool. You will then compile your program in order to create a class that you can then execute in order to run your Java program.

We have run the commands and procedures mentioned in this article on an Ubuntu 18.04 LTS system.

Java Installation

In order to compile and run a basic Java program, you need to have the following two software installed on your Ubuntu system:

Let us first open the Ubuntu command line, the Terminal, in order to install these two. You can open the Terminal either through the system Dash or the Ctrl+alt+T shortcut.

Since we will be installing Java through the apt utility, first let us update our apt repositories through the following command:

The next thing is to run the following command:

This will ensure either Java Runtime Environment is already installed on your system or not. If yes, it will also let you know which version of Java you have on your system.

In my case, the output shows that I do not have Java installed on my system yet.

Java version

In order to install the Java Runtime Environment on your system, run the following command as root:

Install Java default JRE

The system will prompt a Y/n option for you to continue installation. Please enter Y to continue, after which JRE will be installed on your system.

You can then check the installed version of Java as follows:

Check Java version again

After installing the JRE, let us check if we have the Java Development Kit installed on our system or not. This can be done by checking the version of your Java Compiler, javac, as follows:

javac version

The above output shows that I need to install the Java compiler or the JDK on my system.

You can install it through the following command as root:

$ sudo apt install default-jdk

Install Java JDK

The system will prompt a Y/n option for you to continue installation. Please enter Y to continue, after which JDK will be installed on your system.

You can then check the installed version of the Java Compiler as follows:

Check java compiler version again

The basic installations you need to have before running a Java program are now complete.

Your First Java Program

Before starting to write Java programs, it is best practice to make a dedicated directory for all your Java related work. I am creating such a directory through the following command in my home folder:

Then move to that directory as follows:

Create project directory

The next step is to write your first Java program. You can write it in any of your favorite text editors. Here, I am using the gedit editor to write the program. Run the following command in order to open a new java file in the gedit editor:

Open new Java source code file

Then copy the following code in your file:

Java program code

This program is simply intended to print “Hello World” on your screen.

Save the file and close it.

Then is the time to compile your first program through the Java compiler as follows:

The ls command will show that the compiler will create a class based on the class in your Java code:

Compile java sourcecode

In order to run the compiled program, run the following program:

Running Japa program

In this article, you have learned to install both the Java Runtime Environment and the Java Development Kit used to compile and run your java programs. We also wrote a simple Hello World program in Java and ran to see if you and your system are ready to move to the more complex world of Java Programming.

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

Оцените статью