68 lines
3.6 KiB
Plaintext
68 lines
3.6 KiB
Plaintext
<p>Today I encountered a bug that was specific to JDK 16 on a project I was working
|
||
on, and I needed to switch back my Java version to something older. I realized
|
||
I had forgotten (once again) how to switch between multiple Java version on Ubuntu
|
||
(Debian), so I’ve decided to write a short article that would help me remember
|
||
this better.<sup id="fnref:1"><a class="footnote" href="https://batsov.com/articles/2021/12/10/working-with-multiple-versions-of-java-on-ubuntu/#fn:1" rel="footnote">1</a></sup></p>
|
||
|
||
<p>You can install easily multiple version of Java on Ubuntu via <code class="language-plaintext highlighter-rouge">apt</code>:</p>
|
||
|
||
<pre><code class="language-shellsession">$ sudo apt install openjdk-8-jdk openjdk-8-source openjdk-8-doc
|
||
$ sudo apt install openjdk-11-jdk openjdk-11-source openjdk-11-doc
|
||
</code></pre>
|
||
|
||
<p>Typically the newest version of Java you install will become the default, but you can easily change this:</p>
|
||
|
||
<pre><code class="language-shellsession">$ sudo update-alternatives --config java
|
||
There are 2 choices for the alternative java (providing /usr/bin/java).
|
||
|
||
Selection Path Priority Status
|
||
------------------------------------------------------------
|
||
0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
|
||
* 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
|
||
2 /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1081 manual mode
|
||
|
||
Press <enter> to keep the current choice[*], or type selection number:
|
||
</code></pre>
|
||
|
||
<p>Notice that pressing 0 mean “auto-select the newest Java available” (in our case Java 11).
|
||
You can now select Java 8 by pressing 2 and verify the command worked properly like this:</p>
|
||
|
||
<pre><code class="language-shellsession">$ java -version
|
||
openjdk version "1.8.0_292"
|
||
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~20.04-b10)
|
||
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
|
||
</code></pre>
|
||
|
||
<p>You’ll need to repeat the above steps for <code class="language-plaintext highlighter-rouge">javac</code> (the Java compiler binary) as well:</p>
|
||
|
||
<pre><code class="language-shellsession">$ sudo update-alternatives --config javac
|
||
</code></pre>
|
||
|
||
<p>This much I already knew, even if I keep forgetting the exact name of
|
||
<code class="language-plaintext highlighter-rouge">update-alternatives</code>, but today I learned something new as well. You can
|
||
actually simplify the process a bit by using the specialized command
|
||
<code class="language-plaintext highlighter-rouge">update-java-alternatives</code>:</p>
|
||
|
||
<pre><code class="language-shellsession">$ update-java-alternatives -l
|
||
java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64
|
||
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
|
||
|
||
$ sudo update-java-alternatives -s java-1.11.0-openjdk-amd64
|
||
</code></pre>
|
||
|
||
<p>Quite handy! You can also go back to the latest Java version with a shorthand:</p>
|
||
|
||
<pre><code class="language-shellsession">$ sudo update-java-alternatives -a
|
||
</code></pre>
|
||
|
||
<p><code class="language-plaintext highlighter-rouge">-a</code> stands for <code class="language-plaintext highlighter-rouge">--auto</code>, meaning the Java version with highest priority (in our case Java 11 with priority 1111).</p>
|
||
|
||
<p>That’s all I have for you today. Short and sweet!</p>
|
||
|
||
<div class="footnotes">
|
||
<ol>
|
||
<li id="fn:1">
|
||
<p>Or at least look up the information faster. <a class="reversefootnote" href="https://batsov.com/articles/2021/12/10/working-with-multiple-versions-of-java-on-ubuntu/#fnref:1">↩</a></p>
|
||
</li>
|
||
</ol>
|
||
</div> |