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)

If you are using a recent build of Raspbian, then an Oracle Java runtime environment is pre-installed. Nothing more to do.

If you are using an older Raspbian build that does not already include a Java runtime environment, please see the following article for a complete set of installation instructions for installing the Oracle JDK.
http://www.savagehomeautomation.com/pi-jdk

WiringPi Native Library

Pi4J depends on the WiringPi native library 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.


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:

  1. adds the Pi4J APT repository to the local APT repositories
  2. downloads and installs the Pi4J GPG public key for signature validation
  3. invokes the 'apt-get update' command on the Pi4J APT repository to update the local package database
  4. 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.2.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.2.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