Running java program in linux

How to Run a Java Program in Terminal?

Java is the world-famous OOP-based language developed by James Gosling. It runs on multiple operating systems, including macOS, Linux, and Windows. When it comes to Linux, a Java program can be executed through the GUI and Terminal. This guide will demonstrate the possible methods to run a Java program through the terminal.

  • Prerequisite: How to Install Java in Terminal?
  • How to Run a Java Program in a Terminal?
  • How to Run Compiled Java Program Using Class Name in Terminal?
  • Bonus Tip: How to Uninstall Java in Terminal?

Prerequisite: How to Install Java in Terminal?

Java is a platform-independent and highly secure language compared to other programming languages. Before running a program, Java must be installed in the operating system. For this, the command is given below to install java in Debian operating systems:

For CentOS/RedHat Distributions:

$ sudo yum install java-11-openjdk-devel

For Arch Linux:

For Ubuntu/Debian Linux System:

$ sudo apt install default-jdk

How to Run a Java Program in a Terminal?

After the installation, you must confirm that the current system has enabled the Java Development Kit. To do so, the “version” utility is utilized to check the java compiler is installed in the operating system:

The output shows that the current version of java compiler “javac 11.0.17” is installed in the Linux system.

Check Java RunTime Environment

To check the JRE is installed in the operating system, execute the below script:

The environment is ready to run the java program in the terminal.

Читайте также:  Linux mint remote desktop client

(Optional) Install Java Compile

OpenJDK enables the compilation of Java applications. To install the latest version of the compiler, execute the below script:

$ sudo apt-get install openjdk-8-jdk

Java Program

You can open the “Text Editor” or “Nano Editor” and write the code based on your needs. In our case, the code is written in the text editor and saved with the name “program”.

The explanation of the code is given below:

  • A class Student is created on which the main function is defined.
  • In this function, a string “Student are present in class” is written to present in the output via “println()” method.

How to Compile Java Program Using File Name in Terminal?

To compile the java program from the name of the java file such as “program”, execute the below script:

The output verifies that the program has been successfully executed by the name of the class.

How to Run Compiled Java Program Using Class Name in Terminal?

You can run the compiled program from the Ubuntu terminal by specifying the name of the class. In this case, the “Student” is the name of the class accessed through the terminal that will display the output:

The output “Student are present in class” confirms that the compiled java program has been successfully executed from the terminal.

Bonus Tip: How to Uninstall Java in Terminal?

It is an additional step to uninstall the java through the terminal. To do so, execute the below script to delete all dependencies associated with the installed java packages:

$ sudo apt remove --autoremove default-jdk

In this way, you can uninstall the java application from the Ubuntu operating system.

Conclusion

In the Ubuntu terminal, you can run the Java program by executing “javac ” or “java ”. Before execution, you must ensure the Java application is installed in the operating system. Users can execute the complex program in the terminal by following the same procedure. Additionally, the uninstallation method of java is provided here. This article has explained detailed guidelines for running the java program in the Ubuntu terminal.

Источник

How to Run Java from Command-line in Linux

Java is the world’s popular software development platform that James Gosling develops. It is designed to support multiple platforms like Linux, macOS and Windows. Mobile and Desktop applications can also be developed using Java language.

Читайте также:  Альт линукс справки бк

Java language is one of the most popular high-level object-oriented programming languages. It comes with a simple syntax and easily understandable for beginners, as it is very secure and economical to use. Java is the platform-independent software, and it also provides an auto garbage collection facility.

How to Run Java through Command-line

To run the java program in Linux, we need to verify if Java Development Kit (JDK) is available in the system and its version.

To confirm it, type the following command:

(Javac command-line tool is used for the compilation of java programs)

The Javac command tool is not available in my system. We have multiple commands to download it, as mentioned in the above image.

Let’s go with the default-jdk command to get it:

To verify the installation of javac, type:

Now, write a Java program in the text file and save it with .java extension.

Suppose I created a file named “testing.java” and write a simple program in it:

(Keep in mind that your class name should be the same as the file name)

Compile the testing.java file on the terminal using the javac command:

Now, execute the Java program by calling its class name in the terminal:

Conclusion

Java is the high-level language of the modern era supported by the Java Development Kit (JDK). JDK is a package that helps to run java and is used for the development of software packages.

Java language comes with a simple syntax that is easy to get for beginners, and it is one of the most usable object-oriented programming languages.

We have seen in this article that how to install and run Java applications on the terminal.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

How to Run a Command-Line Java Program on Linux?

Run A Java Program Featured

In this tutorial, we will learn about how to run a command-line Java program from the Linux terminal.

Prerequisites

To run Java programs from the command line, a functional Java Runtime Environment is required.

Читайте также:  Iptables persistent astra linux

To install Java Runtime Environment on Ubuntu/Debian based systems:

sudo apt install default-jdk

and to install Java Runtime Environment on CentOS/RHEL based systems:

sudo yum install java-1.8.0-openjdk

To check if a functional Java Runtime Environment is available on your system execute:

Java Version Command Output

The output similar to shown above tells us about a functional Java Runtime Environment available on the system.

Running a Command-Line Java Program in Linux

Follow the steps listed below to get the bytecode from the source file, and to run the Java program.

Step 1: Write the Java program

You can use any text editor (like nano, vim, etc.) to write a Java program and then save it as a .java file. In this tutorial, we have written a “Hello World” Java program with the following source code, saved as a HelloWorld.java file.

Note: The Java source code file name (ending with extension .java) should be the same as the public class name (in here HelloWorld) of the Java program source code. That is why we have named our source code file HelloWorld.java

HelloWorld Java Program

Once we have written and saved our Java program in a .java file, we can now proceed further.

Step 2: Compile your Java program

A Java compiler compiles the source code of a Java program to obtain a bytecode for the particular source code. To compile a Java program source code, the javac command is used.

Compiling Java Program command-line Java

If no error is printed on the screen, then the Java program has been successfully compiled and a new file is obtained (in here the HelloWorld.class is obtained), which is the bytecode file for our Java program.

Obtaining Bytecode command-line Java

Step 3: Run the command-line Java program

Now to run the Java program that we have compiled earlier in the second step.

We use the java command to run the Java program. To run the Java program, execute:

Running A Java Program From Command Line command-line Java

Notice that we haven’t used any file extension while running a Java program using the java command. This is because the Java command can identify the compiled Java files and does not need the .java extension to be added.

Conclusion

You can easily write, compile, and run a Java program from the command line following the steps mentioned above. To run a Java program, the Java Runtime Environment is required which can be installed using your Linux distribution’s official package manager.

Источник

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