{"id":8419,"date":"2026-03-18T01:01:53","date_gmt":"2026-03-18T01:01:53","guid":{"rendered":"https:\/\/commandlinux.com\/?p=8419"},"modified":"2026-03-19T01:07:31","modified_gmt":"2026-03-19T01:07:31","slug":"apt-install-java","status":"publish","type":"post","link":"https:\/\/commandlinux.com\/qa\/apt-install-java\/","title":{"rendered":"How to Apt Install Java on Ubuntu (JRE and JDK)"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"UTF-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n<style>\n*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n.apt-install-java-container a { color: #1a56db; text-decoration: none; border-bottom: 1px solid #c7d7f8; transition: border-color 0.15s; }\n.apt-install-java-container a:hover { border-color: #1a56db; }\n\npre {\n  background: #0f1117;\n  color: #e2e8f0;\n  border-radius: 6px;\n  padding: 1rem 1.2rem;\n  margin: 0.9rem 0 1.2rem;\n  overflow-x: auto;\n  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', monospace;\n  font-size: 0.875rem;\n  line-height: 1.6;\n}\n\ncode {\n  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', monospace;\n  font-size: 0.875em;\n  background: #f1f5f9;\n  color: #1e293b;\n  padding: 0.15em 0.4em;\n  border-radius: 3px;\n}\n\npre code { background: transparent; color: inherit; padding: 0; font-size: inherit; }\n\n.intro { font-size: 1.05rem; line-height: 1.8; color: #2d2d2d; margin-bottom: 1.4rem; }\n\n.prereq-box {\n  border: 1px solid #e2e8f0;\n  border-left: 3px solid #1a56db;\n  border-radius: 4px;\n  padding: 0.9rem 1.1rem;\n  margin: 1rem 0 1.4rem;\n  background: #f8faff;\n  font-size: 0.95rem;\n  color: #334155;\n}\n\n.chart-section { margin: 1.4rem 0; }\n\n.chart-title {\n  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;\n  font-size: 0.8rem;\n  font-weight: 600;\n  text-transform: uppercase;\n  letter-spacing: 0.06em;\n  color: #64748b;\n  margin-bottom: 0.9rem;\n}\n\n.compare-grid {\n  display: grid;\n  grid-template-columns: 1fr 1fr;\n  gap: 1px;\n  background: #e2e8f0;\n  border-radius: 6px;\n  overflow: hidden;\n  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;\n  font-size: 0.875rem;\n}\n\n.compare-header {\n  padding: 0.65rem 1rem;\n  font-weight: 700;\n  font-size: 0.8rem;\n  text-transform: uppercase;\n  letter-spacing: 0.05em;\n}\n\n.col-open { background: #eff6ff; color: #1e40af; }\n.col-oracle { background: #fff7ed; color: #c2410c; }\n\n.compare-row { display: contents; }\n\n.compare-cell {\n  background: #fff;\n  padding: 0.55rem 1rem;\n  color: #334155;\n  display: flex;\n  align-items: center;\n  gap: 0.4rem;\n}\n\n.compare-label {\n  background: #f8fafc;\n  padding: 0.55rem 1rem;\n  color: #64748b;\n  font-size: 0.8rem;\n  font-weight: 600;\n  text-transform: uppercase;\n  letter-spacing: 0.04em;\n  grid-column: 1 \/ -1;\n  border-top: 1px solid #f1f5f9;\n}\n\n.dot-yes { color: #16a34a; font-size: 0.85rem; }\n.dot-no { color: #dc2626; font-size: 0.85rem; }\n.dot-neutral { color: #94a3b8; font-size: 0.85rem; }\n\n.feature-bar-wrap { margin: 1.2rem 0; }\n\n.feature-row {\n  display: flex;\n  align-items: center;\n  gap: 0.8rem;\n  margin-bottom: 0.55rem;\n  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;\n  font-size: 0.85rem;\n}\n\n.feature-label { width: 150px; flex-shrink: 0; color: #475569; }\n\n.bar-track {\n  flex: 1;\n  height: 8px;\n  background: #f1f5f9;\n  border-radius: 99px;\n  overflow: hidden;\n}\n\n.bar-fill {\n  height: 100%;\n  border-radius: 99px;\n  animation: barGrow 0.6s ease-out forwards;\n  transform-origin: left;\n}\n\n@keyframes barGrow {\n  from { width: 0; }\n  to { width: var(--w); }\n}\n\n.bar-jdk { background: #3b82f6; }\n.bar-jre { background: #10b981; }\n.bar-label { width: 60px; font-size: 0.75rem; color: #94a3b8; }\n\n.legend {\n  display: flex;\n  gap: 1.2rem;\n  margin-bottom: 0.7rem;\n  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;\n  font-size: 0.78rem;\n  color: #64748b;\n}\n\n.legend-dot {\n  width: 10px;\n  height: 10px;\n  border-radius: 2px;\n  display: inline-block;\n  margin-right: 4px;\n  vertical-align: middle;\n}\n\n.error-table {\n  width: 100%;\n  border-collapse: collapse;\n  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;\n  font-size: 0.875rem;\n  margin: 1rem 0;\n}\n\n.error-table th {\n  background: #0f1117;\n  color: #e2e8f0;\n  padding: 0.6rem 0.9rem;\n  text-align: left;\n  font-weight: 600;\n  font-size: 0.78rem;\n  text-transform: uppercase;\n  letter-spacing: 0.05em;\n}\n\n.error-table td {\n  padding: 0.6rem 0.9rem;\n  border-bottom: 1px solid #f1f5f9;\n  color: #334155;\n  vertical-align: top;\n}\n\n.error-table tr:last-child td { border-bottom: none; }\n.error-table tr:nth-child(even) td { background: #f8fafc; }\n\n.error-msg { font-family: 'SFMono-Regular', 'Consolas', monospace; font-size: 0.8rem; color: #be123c; }\n\n.faq-answer { font-size: 0.95rem; color: #334155; margin-top: 0.3rem; }\n<\/style>\n<\/head>\n<body>\n\n<p class=\"intro\">Java powers a wide range of applications. Tools such as Tomcat, Cassandra, Jenkins, and GlassFish all depend on it. On Ubuntu, the fastest way to get Java running is to use the <code>apt install java<\/code> command in your terminal. This guide covers OpenJDK, Oracle JDK, version switching, and JAVA_HOME configuration.<\/p>\n\n<h2>What You Need Before Running Apt Install Java<\/h2>\n\n<div class=\"prereq-box\">\nYou need an Ubuntu machine with sudo privileges and terminal access. Make sure your firewall and user account are properly configured before starting.\n<\/div>\n\n<h2>How to Apt Install Java on Ubuntu Using OpenJDK<\/h2>\n\n<p>OpenJDK ships directly from Ubuntu&#8217;s default repositories. It is free, open-source, and functionally identical to Oracle&#8217;s version since Java 11.<\/p>\n\n<p>Start by refreshing your package list:<\/p>\n\n<pre><code>sudo apt update<\/code><\/pre>\n\n<p>Check whether Java already exists on your machine:<\/p>\n\n<pre><code>java -version<\/code><\/pre>\n\n<p>If nothing comes back, you can resolve this in the same way as other <a href=\"https:\/\/commandlinux.com\/how-to\/apt-get-command-not-found\/\">missing apt-get binary issues<\/a> \u2014 verify your package list is current, then proceed. Run the install:<\/p>\n\n<pre><code>sudo apt install default-jre<\/code><\/pre>\n\n<p>This pulls down the Java Runtime Environment. Confirm with:<\/p>\n\n<pre><code>java -version<\/code><\/pre>\n\n<h3>Installing the JDK for Java Development on Ubuntu<\/h3>\n\n<p>If you need the compiler as well \u2014 for building Java programs rather than just running them \u2014 install the full development kit:<\/p>\n\n<pre><code>sudo apt install default-jdk<\/code><\/pre>\n\n<p>Verify the compiler installed correctly:<\/p>\n\n<pre><code>javac -version<\/code><\/pre>\n\n<p>If you need a specific release rather than the latest default, the process for <a href=\"https:\/\/commandlinux.com\/how-to\/apt-install-specific-version\/\">pinning a package to a particular version<\/a> works the same way here \u2014 append the version string to the package name.<\/p>\n\n<h2>OpenJDK vs Oracle JDK: Key Differences<\/h2>\n\n<div class=\"chart-section\">\n  <div class=\"chart-title\">Distribution Comparison<\/div>\n  <div class=\"compare-grid\">\n    <div class=\"compare-header col-open\">OpenJDK<\/div>\n    <div class=\"compare-header col-oracle\">Oracle JDK<\/div>\n\n    <div class=\"compare-label\">License<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-yes\">&#9679;<\/span> Open-source (GPL)<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-neutral\">&#9679;<\/span> Commercial license<\/div>\n\n    <div class=\"compare-label\">Installation<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-yes\">&#9679;<\/span> Ubuntu repositories<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-no\">&#9679;<\/span> Manual download required<\/div>\n\n    <div class=\"compare-label\">Separate JRE Package<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-yes\">&#9679;<\/span> Available<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-no\">&#9679;<\/span> Bundled with JDK<\/div>\n\n    <div class=\"compare-label\">Community Patches<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-yes\">&#9679;<\/span> Yes<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-no\">&#9679;<\/span> No<\/div>\n\n    <div class=\"compare-label\">Functionality (Java 11+)<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-yes\">&#9679;<\/span> Identical<\/div>\n    <div class=\"compare-cell\"><span class=\"dot-yes\">&#9679;<\/span> Identical<\/div>\n  <\/div>\n<\/div>\n\n<h2>How to Get Oracle JDK on Ubuntu<\/h2>\n\n<p>Oracle&#8217;s licensing does not permit direct access to package managers. You must download the archive from Oracle&#8217;s site, transfer it to your server, and rely on a third-party installer script.<\/p>\n\n<p>After downloading the <code>.tar.gz<\/code> file, use <code>scp<\/code> to move it to your server, import the signing key, add the repository, then run the installer with apt. Oracle JDK bundles its own runtime, so a separate JRE package is not needed.<\/p>\n\n<h2>Switching Between Multiple Java Versions on Ubuntu<\/h2>\n\n<p>Ubuntu supports running multiple Java versions at the same time. Use this command to pick your default:<\/p>\n\n<pre><code>sudo update-alternatives --config java<\/code><\/pre>\n\n<p>A numbered list appears. Type the number matching your preferred version and press Enter. You can repeat the same process for <code>javac<\/code>, <code>keytool<\/code>, and <code>javadoc<\/code>.<\/p>\n\n<h2>Setting the JAVA_HOME Variable After Apt Install Java<\/h2>\n\n<p>Many Java-based programs read the <code>JAVA_HOME<\/code> environment variable to locate your installation. First, find the path:<\/p>\n\n<pre><code>sudo update-alternatives --config java<\/code><\/pre>\n\n<p>Copy the directory shown \u2014 excluding the <code>bin\/<\/code> portion at the end. Then open <code>\/etc\/environment<\/code>:<\/p>\n\n<pre><code>sudo nano \/etc\/environment<\/code><\/pre>\n\n<p>Append this line:<\/p>\n\n<pre><code>JAVA_HOME=\"\/usr\/lib\/jvm\/java-11-openjdk-amd64\"<\/code><\/pre>\n\n<p>Reload the file and confirm:<\/p>\n\n<pre><code>source \/etc\/environment\necho $JAVA_HOME<\/code><\/pre>\n\n<p>For a full breakdown of how <a href=\"https:\/\/commandlinux.com\/how-to\/linux-set-environment-variable\/\">persistent environment variables work across sessions<\/a> on Linux, including system-wide vs. user-level scope, that guide covers both approaches in detail.<\/p>\n\n<p>Other users on the system will see the updated value after logging out and back in.<\/p>\n\n<h2>JRE vs JDK: Which One to Install<\/h2>\n\n<div class=\"chart-section\">\n  <div class=\"chart-title\">Capability Coverage<\/div>\n  <div class=\"legend\">\n    <span><span class=\"legend-dot\" style=\"background:#3b82f6;\"><\/span>JDK<\/span>\n    <span><span class=\"legend-dot\" style=\"background:#10b981;\"><\/span>JRE<\/span>\n  <\/div>\n  <div class=\"feature-bar-wrap\">\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Run Java apps<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill bar-jdk\" style=\"--w:100%;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#3b82f6;\">JDK &#10003;<\/div>\n    <\/div>\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Run Java apps<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill bar-jre\" style=\"--w:100%;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#10b981;\">JRE &#10003;<\/div>\n    <\/div>\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Compile source<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill bar-jdk\" style=\"--w:100%;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#3b82f6;\">JDK &#10003;<\/div>\n    <\/div>\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Compile source<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill\" style=\"--w:0%;background:#dc2626;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#dc2626;\">JRE &#10005;<\/div>\n    <\/div>\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Debugger tools<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill bar-jdk\" style=\"--w:100%;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#3b82f6;\">JDK &#10003;<\/div>\n    <\/div>\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Debugger tools<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill\" style=\"--w:0%;background:#dc2626;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#dc2626;\">JRE &#10005;<\/div>\n    <\/div>\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Disk footprint<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill bar-jdk\" style=\"--w:80%;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#3b82f6;\">Larger<\/div>\n    <\/div>\n    <div class=\"feature-row\">\n      <div class=\"feature-label\">Disk footprint<\/div>\n      <div class=\"bar-track\"><div class=\"bar-fill bar-jre\" style=\"--w:40%;\"><\/div><\/div>\n      <div class=\"bar-label\" style=\"color:#10b981;\">Smaller<\/div>\n    <\/div>\n  <\/div>\n<\/div>\n\n<h2>Fixing Common Problems with Apt Install Java<\/h2>\n\n<table class=\"error-table\">\n  <thead>\n    <tr>\n      <th>Error Message<\/th>\n      <th>Fix<\/th>\n    <\/tr>\n  <\/thead>\n  <tbody>\n    <tr>\n      <td><span class=\"error-msg\">command not found<\/span> for java<\/td>\n      <td>Add the JDK <code>bin<\/code> folder to your PATH. See the guide on <a href=\"https:\/\/commandlinux.com\/how-to\/add-to-path-linux\/\">extending the Linux executable search path<\/a> for the correct syntax.<\/td>\n    <\/tr>\n    <tr>\n      <td><span class=\"error-msg\">JAVA_HOME is not set<\/span><\/td>\n      <td>Export <code>JAVA_HOME<\/code> pointing to the JDK directory, excluding <code>\/bin<\/code>.<\/td>\n    <\/tr>\n    <tr>\n      <td><span class=\"error-msg\">Cannot locate JDK or JRE<\/span><\/td>\n      <td>Verify your PATH includes the correct <code>bin<\/code> directory for the installed version.<\/td>\n    <\/tr>\n    <tr>\n      <td><span class=\"error-msg\">Permission denied<\/span> during setup<\/td>\n      <td>Prefix your command with <code>sudo<\/code>.<\/td>\n    <\/tr>\n  <\/tbody>\n<\/table>\n\n<h2>FAQs<\/h2>\n\n<h3>What is the command to apt install Java on Ubuntu?<\/h3>\n<p class=\"faq-answer\">Run <code>sudo apt update<\/code> first, then <code>sudo apt install default-jre<\/code> for the runtime or <code>sudo apt install default-jdk<\/code> for the full development kit including the compiler.<\/p>\n\n<h3>What is the difference between JRE and JDK on Ubuntu?<\/h3>\n<p class=\"faq-answer\">The JRE runs Java programs. The JDK includes the JRE plus a compiler, debugger, and other development tools. Install the JDK if you are writing or compiling Java code.<\/p>\n\n<h3>How do I switch Java versions using apt on Ubuntu?<\/h3>\n<p class=\"faq-answer\">Run <code>sudo update-alternatives --config java<\/code>, then enter the number corresponding to the version you want. Repeat for <code>javac<\/code> if needed.<\/p>\n\n<h3>Does apt install Java set JAVA_HOME automatically?<\/h3>\n<p class=\"faq-answer\">No. You must set it manually by adding <code>JAVA_HOME=\"\/usr\/lib\/jvm\/java-11-openjdk-amd64\"<\/code> to <code>\/etc\/environment<\/code>, then running <code>source \/etc\/environment<\/code>.<\/p>\n\n<h3>Can I install Oracle JDK with apt on Ubuntu?<\/h3>\n<p class=\"faq-answer\">Not directly. Oracle&#8217;s licensing requires a manual download from Oracle&#8217;s website and a third-party installer script. OpenJDK is functionally identical for most use cases since Java 11.<\/p>\n\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>Java powers a wide range of applications. Tools such as Tomcat, Cassandra, Jenkins, and GlassFish all depend on it. On Ubuntu, the fastest way to get Java running is to use the apt install java command in your terminal. This guide covers OpenJDK, Oracle JDK, version switching, and JAVA_HOME configuration. What You Need Before Running<\/p>\n","protected":false},"author":1,"featured_media":8420,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-8419","post","type-post","status-publish","format-standard","has-post-thumbnail","category-qa"],"_links":{"self":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/8419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/comments?post=8419"}],"version-history":[{"count":2,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/8419\/revisions"}],"predecessor-version":[{"id":8426,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/8419\/revisions\/8426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media\/8420"}],"wp:attachment":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media?parent=8419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/categories?post=8419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/tags?post=8419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}