Back to Java
2026-03-068 min read

Linux (Java)

Learn Linux (Java) step by step with clear examples and exercises.

Title: Linux (Java) - A full guide to Running Java Applications on CentOS

Why This Matters

In today's tech-driven world, mastering various platforms and programming languages is essential for both beginners and professionals. One such platform is the Linux operating system, which powers numerous servers worldwide. To make the most of Linux, it's crucial to understand how to run Java applications on this platform, especially when working with CentOS. This guide will walk you through the core concepts, provide a worked example, discuss common mistakes, and offer practice questions to help you solidify your understanding.

By learning how to run Java applications on CentOS, you'll gain valuable skills that can be applied in various professional settings, such as web development, system administration, and software engineering.

Prerequisites

To follow along with this tutorial, you should have:

  1. Basic knowledge of Linux commands and file navigation. Familiarity with essential Linux utilities such as yum, cd, and mkdir is expected.
  2. Familiarity with Java programming concepts, including classes, methods, and packages.
  3. A CentOS system or a virtual machine running CentOS 7.
  4. The java command installed on your CentOS system (if not installed, follow the instructions in the "Install JDK" section below).
  5. Understanding of text editors like nano or vim for creating Java files.
  6. Familiarity with build tools such as Maven or Gradle (optional but recommended for managing dependencies).

Core Concept

To run Java applications on CentOS, you'll need to have the Java Development Kit (JDK) installed. This tutorial will guide you through installing JDK, setting up a simple Java application, and discussing classpath, JAR files, and dependencies management using Maven or Gradle.

Install JDK

CentOS 7 uses the yum package manager for software installation. To install OpenJDK, run the following command:

sudo yum install java-1.8.0-openjdk -y

Replace 1.8.0 with the desired Java version if needed.

Create a Java Application

Create a new directory for your project and navigate to it:

mkdir myJavaApp && cd myJavaApp

Next, create a file named HelloWorld.java containing the following code:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Open this file using your preferred text editor (e.g., nano HelloWorld.java) and save it.

Compile and Run the Java Application

Compile the Java file using the javac command:

javac HelloWorld.java

This will generate a class file named HelloWorld.class. To run the application, use the java command followed by the name of the class:

java HelloWorld

You should see "Hello, World!" displayed in your terminal.

Understanding Classpath and JAR Files

When running Java applications, you may encounter issues related to classpaths or need to package multiple classes into a single executable file (JAR). To address these scenarios, let's discuss classpath and JAR files.

Classpath

The classpath is a list of directories and JAR files that the Java Virtual Machine (JVM) searches for class files when running an application. You can set the classpath using the -cp or -classpath option:

java -cp . HelloWorld # Runs the application in the current directory
java -cp /path/to/jarfile HelloWorld # Runs the application from a JAR file

JAR Files

To package multiple classes into a single executable JAR file, use the jar command:

  1. Create a MANIFEST.MF file (optional) to include version information or other metadata.
  2. Compile all Java files in your project.
  3. Package the compiled class files and MANIFEST.MF (if created) into a JAR file using the jar command:
jar cvfm myJavaApp.jar MANIFEST.MF *.class .

Now you can run the application by executing the JAR file:

java -jar myJavaApp.jar

Managing Dependencies with Maven or Gradle

When working with Java applications, it's common to have dependencies on external libraries. To manage these dependencies in a CentOS environment, consider using Maven or Gradle build tools. These tools simplify the process of downloading and managing dependencies for your project.

Using Maven

Create a pom.xml file in your project directory with the following content:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>myJavaApp</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- Add any required dependencies here -->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<finalName>myJavaApp</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>

Replace com.example with a suitable group ID for your project.

To download and manage dependencies, run the following command:

mvn dependency:get

Now you can package the application into a JAR file using Maven:

mvn clean package

Using Gradle

Create a build.gradle file in your project directory with the following content:

plugins {
id 'java'
}

group = 'com.example'
version = '1.0-SNAPSHOT'

dependencies {
// Add any required dependencies here
}

jar {
manifest {
attributes 'Main-Class': 'HelloWorld'
}
}

Replace com.example with a suitable group ID for your project.

To download and manage dependencies, run the following command:

gradle download

Now you can package the application into a JAR file using Gradle:

gradle jar

Worked Example

Suppose you have a project with multiple Java files, and you want to package them into a single JAR file and run it on CentOS. Here's how to do it using Maven:

  1. Create a new directory for your project and navigate to it:
mkdir myMultiClassApp && cd myMultiClassApp
  1. Create three Java files named Main.java, Util.java, and Config.java. Add the following code to each file:

Main.java:

public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
Util.someMethod();
// Load configuration from Config.class
}
}

Util.java:

public class Util {
public static void someMethod() {
System.out.println("This is a utility method.");
}
}

Config.java:

public class Config {
// Configuration data
}
  1. Create a pom.xml file in your project directory with the following content:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>myMultiClassApp</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- Add any required dependencies here -->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<finalName>myMultiClassApp</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
  1. Compile all Java files:
mvn compile
  1. Package the compiled class files into a JAR file using Maven:
mvn clean package

Now you can run the application by executing the JAR file:

java -jar target/myMultiClassApp.jar

Common Mistakes

  1. Forgetting to set the classpath when running a Java application from the terminal.
  2. Not including the Main-Class attribute in the JAR file's manifest when using Gradle or Maven.
  3. Failing to compile Java files before attempting to run them.
  4. Overlooking the need to download and manage dependencies when working with larger projects.
  5. Neglecting to handle exceptions properly, leading to unhandled errors during runtime.

Practice Questions

  1. What is the role of the pom.xml file in a Maven-based Java project on CentOS?

Answer: The pom.xml file contains metadata about your project, including its group ID, artifact ID, version, dependencies, and build settings. It's used by Maven to manage your project and its dependencies.

  1. How do I create a JAR file using Maven or Gradle in CentOS?

Answer: To create a JAR file using Maven, follow the steps outlined in the "Managing Dependencies with Maven" section. For Gradle, create a build.gradle file and run the gradle jar command.

  1. How do I handle dependencies when working with a CentOS environment and Java applications?

Answer: Use Maven or Gradle to manage dependencies in your project. These tools simplify the process of downloading and managing external libraries.

  1. What is the purpose of the Util class in the worked example, and what method does it contain?

Answer: The Util class contains a utility method that demonstrates how to call methods from another class within the same project. In this case, the method is named someMethod().

  1. How do I run a Java application from the terminal in CentOS?

Answer: First, compile your Java files using javac, then execute the class file using the java command followed by the name of the class. If you have multiple classes, set the classpath appropriately using the -cp or -classpath option.

FAQ

What is the difference between JDK and JRE?

Answer: JDK (Java Development Kit) includes everything needed to develop Java applications, while JRE (Java Runtime Environment) only provides the runtime environment for executing Java programs.

How do I install JDK on CentOS?

Answer: Install OpenJDK using the yum package manager as shown in the "Install JDK" section above.

What is a classpath, and how do I set it?

Answer: The classpath is a list of directories and JAR files that the Java Virtual Machine (JVM) searches for class files when running an application. You can set the classpath using the -cp or -classpath option when executing the Java command.

What are Maven and Gradle, and why should I use them?

Answer: Maven and Gradle are build automation tools for Java projects that simplify the process of managing dependencies, compiling, testing, and packaging your application. They help streamline the development process by automating repetitive tasks.

How do I handle exceptions in a Java program?

Answer: Use try-catch blocks to catch exceptions and handle them appropriately. You can also use exception handling

Linux (Java) | Java | XQA Learn