How to Install and Set Up Java on a VPS with AnonVM

Java is a versatile programming language widely used for building web applications, backend systems, and mobile apps. Installing Java on your VPS is essential if you’re looking to deploy Java-based applications on AnonVM. This guide will walk you through installing Java Development Kit (JDK) and setting up Java on your AnonVM VPS.


System Requirements

Before you begin, ensure that your VPS meets the following requirements:

  • OS: Ubuntu 18.04+, Debian, or CentOS 7+
  • RAM: Minimum of 1GB recommended
  • Access: Root or sudo user privileges

Step 1: Update Your System

To avoid any compatibility issues, start by updating your system’s package manager.

For Ubuntu/Debian:

 
sudo apt update && sudo apt upgrade -y

For CentOS:

 
sudo yum update -y

Step 2: Choose the Right Java Version

Java comes in several versions, including:

  1. Java SE 8 - Widely supported and used for many applications.
  2. Java SE 11 (LTS) - Long-term support release.
  3. Java SE 17 (LTS) - Latest LTS version, highly recommended.

Most software supports Java 8 or later, but Java 17 is recommended for long-term projects.


Step 3: Install Java on Ubuntu/Debian

Option 1: Install OpenJDK from Default Repositories

To install the OpenJDK version 11 (for example):

 
sudo apt install -y openjdk-11-jdk

To install Java 17:

 
sudo apt install -y openjdk-17-jdk

Option 2: Install Oracle JDK

For Oracle JDK, you need to download the binaries manually. Visit Oracle's Java Downloads page and follow the instructions for installation.


Step 4: Install Java on CentOS

  1. Enable OpenJDK repository (Java 11):

     
    sudo yum install -y java-11-openjdk
  2. Verify the installation:

     
    java -version
  3. Alternative - Install Oracle JDK on CentOS

    Download the RPM file from Oracle's website and install it manually.


Step 5: Set Java Environment Variables

After installing Java, you should configure environment variables such as JAVA_HOME and PATH to make it easier to run Java commands globally.

  1. Locate Java Installation Path:

    Use the following command to find the path to the installed Java version:

     
    sudo update-alternatives --config java

    The output will show the installation path (e.g., /usr/lib/jvm/java-11-openjdk-amd64).

  2. Set JAVA_HOME and PATH Variables:

    Add the following lines to your .bashrc file:

     
    echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> ~/.bashrc echo "export PATH=\$JAVA_HOME/bin:\$PATH" >> ~/.bashrc source ~/.bashrc

    Replace the path /usr/lib/jvm/java-11-openjdk-amd64 with the path from the previous step.

  3. Verify Environment Variables:

     
    echo $JAVA_HOME echo $PATH

    Both should show the correct paths, confirming that the environment variables are correctly set.


Step 6: Testing Your Java Installation

To verify your Java installation, create a simple Java program and compile it.

  1. Create a Test Java File:

     
    nano HelloWorld.java

    Paste the following code:

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

     
    javac HelloWorld.java
  3. Run the Program:

     
    java HelloWorld

    If everything is installed correctly, you should see:

    plaintext
     
    Hello, World!

Step 7: Install Maven or Gradle (Optional)

Maven and Gradle are popular build automation tools for Java. They manage dependencies and streamline building, testing, and deploying applications.

Installing Maven

  1. Install Maven on Ubuntu:

     
    sudo apt install -y maven
  2. Install Maven on CentOS:

     
    sudo yum install -y maven
  3. Verify Maven Installation:

     
    mvn -version

Installing Gradle

  1. Download Gradle:

     
    wget https://services.gradle.org/distributions/gradle-7.5-bin.zip
  2. Extract Gradle:

     
    sudo unzip gradle-7.5-bin.zip -d /opt/gradle
  3. Add Gradle to PATH:

     
    echo "export PATH=/opt/gradle/gradle-7.5/bin:\$PATH" >> ~/.bashrc source ~/.bashrc
  4. Verify Gradle Installation:

     
    gradle -v

Step 8: Deploy a Sample Java Application (Optional)

To confirm that your environment is fully set up, try deploying a Java application, either manually or using Maven/Gradle.

Using Maven

  1. Create a Maven Project:

     
    mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false cd my-app
  2. Compile the Project:

     
    mvn package
  3. Run the Application:

     
    java -cp target/my-app-1.0-SNAPSHOT.jar com.example.App

    You should see the output of your sample Maven application.


Conclusion

Java is now installed and configured on your VPS with AnonVM. You have set up Java, verified the installation, and optionally configured Maven or Gradle for building Java applications. You’re ready to develop, test, and deploy Java applications on your server! For more advanced projects, consider setting up a CI/CD pipeline to automate builds and deployments.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution