杰瑞科技汇

Ubuntu 14.04如何安装配置Java?

Of course. Here is a comprehensive guide to installing and managing Java on Ubuntu 14.04.

Ubuntu 14.04如何安装配置Java?-图1
(图片来源网络,侵删)

⚠️ Important Warning: Ubuntu 14.04 is End-of-Life (EOL)

This is the most critical point you must understand.

  • No Security Updates: Ubuntu 14.04 reached its official End of Life in April 2025. This means it no longer receives any security patches, bug fixes, or software updates from Canonical.
  • Vulnerabilities: Running an EOL system on the internet is extremely dangerous. It leaves you wide open to known security exploits.
  • Unsupported Software: Most modern software, including newer versions of Java, will not run on or support Ubuntu 14.04. The official repositories for Java have been removed.

Strong Recommendation: You should upgrade to a supported, Long-Term Support (LTS) version of Ubuntu, such as Ubuntu 20.04 LTS or Ubuntu 22.04 LTS. This is the only safe and sustainable way to use your system.

If you absolutely must use Ubuntu 14.04 for a specific, isolated, and non-critical purpose, you can proceed with the instructions below, but be aware of the significant risks.


Methods to Install Java on Ubuntu 14.04

Here are the three primary methods, ordered from most recommended (for an EOL system) to least recommended.

Ubuntu 14.04如何安装配置Java?-图2
(图片来源网络,侵删)

Method 1: Install from a Third-Party PPA (Recommended for EOL Systems)

Since the official Ubuntu repositories are empty for Java, the best way to get a recent version is to use a Personal Package Archive (PPA). A popular and well-maintained PPA for Java is webupd8team/java.

This method allows you to easily install Oracle Java JDK, which is the standard for many development environments.

Step 1: Add the WebUpd8 Java PPA

Open a terminal and add the repository:

Ubuntu 14.04如何安装配置Java?-图3
(图片来源网络,侵删)
sudo add-apt-repository ppa:webupd8team/java

You will be prompted to press Enter to continue.

Step 2: Update Your Package List

Now, update your system's package list to include the new PPA's packages:

sudo apt-get update

Step 3: Install Oracle Java JDK (or JRE)

You will be presented with a license agreement during the installation. You must accept it to proceed.

To install the JDK (Java Development Kit): The JDK includes the JRE plus the development tools (compiler, debugger, etc.) needed for programming.

sudo apt-get install oracle-java8-installer

You can also try oracle-java11-installer, but java8 is more likely to be stable and compatible on the old 14.04 system.

To install only the JRE (Java Runtime Environment): The JRE is only needed to run Java applications, not to develop them.

sudo apt-get install oracle-java8-set-default

Step 4: Verify the Installation

Check the installed version to confirm it worked:

java -version

You should see output similar to this, confirming the Oracle Java version:

java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)

You can also check if the JAVA_HOME environment variable was set correctly:

echo $JAVA_HOME

It should point to the installation directory, e.g., /usr/lib/jvm/java-8-oracle.


Method 2: Manual Installation (Download and Set Up)

This method gives you full control and is useful if you need a specific version not available in the PPA or if you prefer not to use third-party repositories.

Step 1: Download the JDK

Go to the Oracle Java SE Development Kit 8 Downloads page. https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html

Accept the license agreement and download the .tar.gz file for Linux (e.g., jdk-8u321-linux-x64.tar.gz).

Step 2: Create a Directory for Java

It's good practice to create a directory for Java under /usr/local.

sudo mkdir -p /usr/lib/jvm

Step 3: Extract the JDK

Move the downloaded file to the /usr/lib/jvm directory and extract it.

# Move the file (replace with your actual filename)
sudo mv ~/Downloads/jdk-8u321-linux-x64.tar.gz /usr/lib/jvm/
# Navigate to the directory
cd /usr/lib/jvm/
# Extract the archive
sudo tar -xvzf jdk-8u321-linux-x64.tar.gz

This will create a directory like /usr/lib/jvm/jdk1.8.0_321/.

Step 4: Set Up Environment Variables

You need to tell your system where to find Java.

  1. Set JAVA_HOME: Open the /etc/environment file with a text editor like nano:

    sudo nano /etc/environment

    Add the following line at the end of the file, replacing the path with your actual JDK path:

    JAVA_HOME="/usr/lib/jvm/jdk1.8.0_321"

    Save the file (Ctrl+O, then Enter) and exit (Ctrl+X).

  2. Update the PATH variable: In the same /etc/environment file, find the PATH variable and add $JAVA_HOME/bin to it. It should look something like this:

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$JAVA_HOME/bin"

Step 5: Update update-alternatives

This command helps manage different Java versions on your system.

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_321/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_321/bin/javac" 1

Now, configure the alternatives:

sudo update-alternatives --config java
sudo update-alternatives --config javac

Select the number corresponding to your newly installed Java version.

Step 6: Verify the Installation

Reboot your system or log out and log back in for the environment variables to take full effect, then run:

java -version

Method 3: Using the Old OpenJDK (Not Recommended)

The OpenJDK packages (openjdk-7-jdk, etc.) that were originally in the Ubuntu 14.04 repositories are very old (Java 7) and have long since been removed. You cannot install them via apt-get anymore because the package servers no longer provide them for 14.04. This method is no longer viable.

Summary and Final Recommendation

Method Pros Cons Recommendation
PPA (WebUpd8) Easy, automatic updates via apt, installs a modern version (Java 8). Relies on a third-party PPA, still runs on an insecure OS. Best option for a temporary, isolated 14.04 setup.
Manual Install Full control, no third-party repos, can install any version. More complex, manual updates required, JAVA_HOME must be set manually. Good for specific needs or advanced users.
Old OpenJDK Was the "standard" way. No longer possible. Packages are gone. Do not use.

Final advice: If this is for a production system, a server, or anything connected to a network, stop using Ubuntu 14.04 immediately and upgrade to a supported version. If it's for a personal hobby project on an air-gapped machine, Method 1 is your safest bet for getting Java working.

分享:
扫描分享到社交APP
上一篇
下一篇