Skip to content

Instantly share code, notes, and snippets.

@NotAlexNoyle
Last active March 29, 2021 14:37
Show Gist options
  • Save NotAlexNoyle/c3b8c342105d700846825af838050199 to your computer and use it in GitHub Desktop.
Save NotAlexNoyle/c3b8c342105d700846825af838050199 to your computer and use it in GitHub Desktop.
A step-by-step tutorial on installing OpenJDK 12 on a Raspberry Pi 3 running Raspbian
Do you use your Raspberry Pi 3 to develop Java applications using OpenJDK? You may have noticed that the latest version available on the default apt repositories is OpenJDK 9. This is because the armhf architecture is underserved by the Oracle open source community. Luckilly, you can build and run the latest version (OpenJDK 12.x) on your device. It will just take some time, given the minimal power of the Pi 3's CPU.
The first step is to boot into raspbian and launch the shell.
Next, we are going to create 1GB of swap space for the build.
Get the 'pv' package:
> sudo apt-get install pv
Create the swap space:
> sudo dd if=/dev/zero bs=1M count=1024 | pv | sudo dd of=/var/SWAPFILE
Add it to your system:
> sudo mkswap /var/SWAPFILE
Edit the following lines in /etc/dphys-swapfile with VIM or a similar plain text editor:
CONF_SWAPFILE=/var/SWAPFILE
CONF_SWAPSIZE=1024
Reboot to apply changes:
> sudo reboot
Next we need to install the latest available pre-built OpenJDK, in order to build the next major version. To get to OpenJDK 12 eventually, we first need to install the package that contains OpenJDK 9, then we will use that to build OpenJDK 10, which will be used to build 11, then 11 will be used to build 12, and so on from there. Upon future releases this process can theoretically be continued.
Install OpenJDK 9 from the official repos:
> sudo apt-get install openjdk-9-jdk
Install the remaining build dependencies:
> sudo apt-get install autoconf build-essential libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev libcups2-dev libasound2-dev libfontconfig1-dev zip mercurial libxrandr-dev x11proto-randr-dev
Run the following to pull down the latest OpenJDK 10 source code:
> hg clone http://hg.openjdk.java.net/jdk-updates/jdk10u
Change directories into the cloned OpenJDK 10 Repository:
> cd jdk10u/
Make the configure file executable
> chmod +x configure
Configure the project to prepare for building:
> ./configure --disable-warnings-as-errors --with-native-debug-symbols=none --with-version-pre="armhf" --with-version-build=10
Build the project (this will take hours):
> make LOG=cmdlines images
Install the project:
> sudo make install
Test it:
> java -version
You should see something similar to this:
openjdk 10.0.1-armhf 2018-04-17
OpenJDK Runtime Environment (build 10.0.1-armhf+10)
OpenJDK Server VM (build 10.0.1-armhf+10, mixed mode)
Next, we will clean up.
> cd ..
> sudo rm -rf jdk10u/
Now pull down OpenJDK 11, which we will build with OpenJDK 10:
> hg clone http://hg.openjdk.java.net/jdk-updates/jdk11u/
Change directories into the cloned OpenJDK 11 Repository:
> cd jdk11u
Make the configure file executable
> chmod +x configure
Configure the project to prepare for building:
> ./configure --disable-warnings-as-errors --with-native-debug-symbols=none --with-version-pre="armhf" --with-version-build=11
Build the project (this will take hours):
> make LOG=cmdlines images
Install the project:
> sudo make install
Test it:
> java -version
You should see something like this:
openjdk version "11.0.3-armhf" 2019-04-16
OpenJDK Runtime Environment (build 11.0.3-armhf+11-adhoc.pi.jdk11u)
OpenJDK Server VM (build 11.0.3-armhf+11-adhoc.pi.jdk11u, mixed mode)
Next, we will clean up.
> cd ..
> sudo rm -rf jdk10u/
Now pull down OpenJDK 12, which we will build with OpenJDK 11:
> hg clone http://hg.openjdk.java.net/jdk-updates/jdk12u/
Change directories into the cloned OpenJDK 12 Repository:
> cd jdk12u
Make the configure file executable
> chmod +x configure
Configure the project to prepare for building:
> ./configure --disable-warnings-as-errors --with-native-debug-symbols=none --with-version-pre="armhf" --with-version-build=12
Build the project (this will take hours):
> make LOG=cmdlines images
Install the project:
> sudo make install
Test it:
> java -version
That's it! You now have a Raspberry Pi 3 with OpenJDK 12. Code away!
- NotAlexNoyle
Credit to Jim Connors from oracle for his tutorial [here](https://blogs.oracle.com/jtc/build-jdk-10-for-your-raspberry-pi-right-on-your-device) that was very helpful in coming up with this one!
@Tancred423
Copy link

Pretty cool, but it takes soooo much storage(?). I just wiped my RPi and the only thing that was installed is Raspbian. Then I followed your guide and on cloning the jdk 12 repo my SD card was full and the cloning stopped. I have a 16 GB SD card which should be enough to run an OS and Java, shouldn't it?

@NotAlexNoyle
Copy link
Author

NotAlexNoyle commented Jun 9, 2019

Pretty cool, but it takes soooo much storage(?). I just wiped my RPi and the only thing that was installed is Raspbian. Then I followed your guide and on cloning the jdk 12 repo my SD card was full and the cloning stopped. I have a 16 GB SD card which should be enough to run an OS and Java, shouldn't it?

Did you remember to 'rm -rf' the prior versions of OpenJDK once you were finished using them to build the next sequential version? That might be the cause of your storage woes.

The entire OpenJDK12 folder, upon completion of building, should be just under 300MB.

Perhaps use a space analysis tool to see what is taking up so much space?

Worst case, I can send you a copy of my pre-built OpenJDK 12 that will definitely fit on your 16GB SD Card. I myself use a 32GB.

@Tancred423
Copy link

Pretty cool, but it takes soooo much storage(?). I just wiped my RPi and the only thing that was installed is Raspbian. Then I followed your guide and on cloning the jdk 12 repo my SD card was full and the cloning stopped. I have a 16 GB SD card which should be enough to run an OS and Java, shouldn't it?

Did you remember to 'rm -rf' the prior versions of OpenJDK once you were finished using them to build the next sequential version? That might be the cause of your storage woes.

The entire OpenJDK12 folder, upon completion of building, should be just under 300MB.

Perhaps use a space analysis tool to see what is taking up so much space?

Worst case, I can send you a copy of my pre-built OpenJDK 12 that will definitely fit on your 16GB SD Card. I myself use a 32GB.

Thank you for the answer. I did every step carefully, so I definitely did the 'rm -rf' command line. Somehow I managed to get to openjdk 11 without losing to much space and for now, that should be fine. But of course, OpenJDK 12 would be better for the future. I might try again later as I recently bought a 32 GB SD card. If I fail again, I will come back to your offer to send me a copy, but I hope that is not needed :)

I also found an instruction, that says to just use 'sudo apt-get install openjdk-11-jdk-headless' for openjdk 11. Do you know if that works just as fine? Because then, you don't have to get 9 and upgrade up to 11 (or 12).

@adichad
Copy link

adichad commented Nov 8, 2019

faster, smaller and better (a GA tagged revision indicates high confidence in stability) to download the GA tagged revision for each jdk you want to install. for e.g.:
http://hg.openjdk.java.net/jdk-updates/jdk12u/archive/7b6accc7c009.tar.gz
which you can find by selecting the latest GA tag from here: http://hg.openjdk.java.net/jdk-updates/jdk12u/tags

also, on Raspbian buster as of the date of writing, the default preinstalled jdk version is 11.0.5. however downloading previous versions may be required depending on which version is available via the distribution package manager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment