Pi4J Installation
The following installation instructions are provided to install, upgrade, and uninstall the Pi4J libraries, examples, utility scripts, etc. directly on your Rasbperry Pi. These instructions are not intended to install Pi4J on your development workstation or other computer. For development conducted on a computer other than the Raspberry Pi, please access the Pi4J JAR dependencies via the Maven repository or download them directly and reference them in your project.
Prerequisites
Java Runtime (JRE/JDK)
Java 11 JRE/JDK - The Java 11 runtime environment must be installed and configured on your Raspberry Pi.
You can use the following shell command on your Raspberry Pi to download and install the OpenJDK (11) JDK distribution.
sudo apt-get install openjdk-11-jdk
ATTENTION:
This project requires Java 11 (JDK) to build; however, it fails to compile using the latest Oracle JDKs. The project will successfully compile using OpenJDK, Liberica, Zulu and AdoptOpenJDK distributions.
WiringPi Native Library
Pi4J depends on the WiringPi native library originally created by by Gordon Henderson. Please see the following page for more information on WiringPi.
http://wiringpi.com/
The Pi4J native library is dynamically linked to WiringPi and thus you must have WiringPi installed on your target Raspberry Pi system.
ATTENTION: The original WiringPi library has been **DEPRECATED** and is no longer maintained.
To support RaspberryPi 4B/400 and newer systems you must install the latest *unofficial* WiringPi version which is maintained here: https://github.com/WiringPi/WiringPi.
If you have Pi4J installed on your RaspberryPi, you can use the following script command to download, build and install the latest unofficial build of WiringPi.
sudo pi4j --wiringpi
Alternatively, you can use the following shell commands to download, build and install the latest unofficial build of WiringPi.
sudo apt-get remove wiringpi -y sudo apt-get --yes install git-core gcc make cd ~ git clone https://github.com/WiringPi/WiringPi --branch master --single-branch wiringpi cd ~/wiringpi sudo ./build
Installation
Easy/Preferred
(NOTE: This installation method requires that your RaspberryPi is connected to the Internet.)
The simplest method to install Pi4J on your RaspberryPi is to execute the following command directly on your RaspberryPi.curl -sSL https://pi4j.com/install | sudo bash
This method will download and launch an installation script that perform the following steps:
- adds the Pi4J APT repository to the local APT repositories
- downloads and installs the Pi4J GPG public key for signature validation
- invokes the 'apt-get update' command on the Pi4J APT repository to update the local package database
- invokes the 'apt-get install pi4j' command to perform the download and installation
Offline/Manual
If you prefer/need to install Pi4J on a RaspberryPi device without an Internet connection, the following instructions provide the steps necessary to install Pi4J without requiring an Internet connection.
First, download a copy of the latest Pi4J Debian/Raspian installer package (.deb) file to your local computer. You can download the Pi4J Debian/Raspian installer package (.deb) using your web browser at the following URL:
https://pi4j.com/download/pi4j-1.4.deb
Next, you will need to transfer the download installer package over to your RaspberryPi. You can use any method you prefer to transfer the file (USB, SCP, FTP, etc.)
(NOTE: If you have a previous version of Pi4J installed, you will need to uninstall it first.)
Once the installer package is available on your RaspberryPi, use the following command on the Pi to perform the installation: sudo dpkg -i pi4j-1.4.deb
Upgrade
Easy/Preferred
If you originally installed Pi4J using the 'easy' method, then Pi4J upgrades will be available anytime you perform a system update using 'sudo apt-get update' and 'sudo update-get upgrade'.
If you wish to force an upgrade of the Pi4J package only, you can do so by executing the following command: sudo apt-get install pi4j
or pi4j --update
Offline/Manual
If you originally installed Pi4J using the 'offline' method, then you will need to manually uninstall the Pi4J package and download, transfer, and install the new version package using the 'offline' uninstall and installation methods described here on this page.
Uninstall
Easy/Preferred
If you originally installed Pi4J using the 'easy' method, then you can uninstall Pi4J simply by executing the following command on your RaspberryPi. sudo apt-get remove pi4j
or pi4j --uninstall
Complete/Full Removal
If you originally installed Pi4J using the 'easy' method and you want to remove all traces of Pi4J, including the Pi4J repository in the APT repositories list and the Pi4J GPG signature, then simply execute the following command on your RaspberryPi. curl -sSL https://pi4j.com/uninstall | sudo bash
Offline/Manual
If you originally installed Pi4J using the 'offline' method, then you will need to manually uninstall the Pi4J package by executing the following command on your Raspberry Pi: sudo dpkg -r pi4j
Installed Location / Example Files
This will install the Pi4J libraries and example source files to: /opt/pi4j/lib
/opt/pi4j/examples
When attempting to compile a Java program using the Pi4J libraries, make sure to include the Pi4J lib folder in the classpath: javac -classpath .:classes:/opt/pi4j/lib/'*' ...
When attempting to start a Java program using the Pi4J libraries, make sure to include the Pi4J lib folder in the classpath: sudo java -classpath .:classes:/opt/pi4j/lib/'*' ...
If you would like to explore the examples, you can compile all the examples with the following commands: /opt/pi4j/examples/build