The software can be downloaded from this site: https://pi4j.com/1.2/index.html which has a pointer to the github repository https://github.com/Pi4J/pi4j/tree/release/1.2
Once you cloned the project, you can use Maven to start the compilation. Make sure Maven is installed on your PC. If not, download Maven from here: https://maven.apache.org/download.cgi.
Download the .zip or .tar.gz file and unzip on your Windows machine. The Maven executable is located in the bin directory, so make sure this directory is part of your PATH environment variable. This way, you can launch Maven from everywhere on your PC.
Move over to the Pi4J repository on your PC. You will see the presence of a pom (Project Object Model) file. This pom file is the guidance for Maven to build a project. In this case: Pi4J.
Make sure you have set the correct JDK version before running Maven. For Pi4J, JDK 1.8 is needed, so install this version of Java first, if not done already.
I've created a batch file to easy the build process. This is the content:
set JAVA_HOME=D:\WinApps\Programming\Java\Oracle\jdk_1.8
call mvn clean install
pause
First, the JAVA_HOME is set to the location of JDK 1.8. Next, a call is done to start Maven with the parameters clean and install. This means that previous builds will be removed and the results will be put in a directory.
For Pi4J, this directory is pi4j-distribution\target\distro-contents\lib. The following files can be found in that directory:
.\pi4j-distribution\target\distro-contents\lib\junit.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-core-javadoc.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-core-sources.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-core.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-device-javadoc.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-device-sources.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-device.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-example-javadoc.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-example-sources.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-example.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-gpio-extension-javadoc.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-gpio-extension-sources.jar
.\pi4j-distribution\target\distro-contents\lib\pi4j-gpio-extension.jar
As you can see sources, documentation and binary files are created.
Building Pi4J 1.4 is similar to building Pi4J 1.2. However, there are a few big changes done compared to Pi4J 1.2.
One of the most dramatic decisions that have been made is the removal of the following two modules:
pi4j-device
pi4j-gpio-extension
I can understand the Pi4J guys didn't want to support anymore the exotic Raspberry Pi derivatives like OrangePi and the ones, but I can't understand why they dropped such a beautiful add-ons to the Pi4J framework.
Since those modules will not be part of the distro again, each and everyone will start making their own versions of things that were there in the past and that have proven to be working correct! It will become chaotic, I'm afraid.
Luckily, I found a way to still add those two useful modules to the Pi4J 1.4 framework. Here is what I did:
Copied the two modules pi4j-device and pi4j-gpio-extension to the Pi4j 1.4 environment "as is".
I modified the pom.xml file of the Pi4J 1.4 build mechanism to include those two extra modules; therefore I added the following info into the main pom.xml file within the section <modules>:
<module>pi4j-gpio-extension</module>
<module>pi4j-device</module>
This is how the section <modules> looks like now:
<!-- PROJECT MODULES -->
<modules>
<module>pi4j-core</module>
<module>pi4j-example</module>
<module>pi4j-gpio-extension</module>
<module>pi4j-device</module>
<module>pi4j-distribution</module>
</modules>
The result of this action is that both modules are again taken into account in the build process. However, I did not succeed to have the .jar files for both of them as part of the final distribution (located in ./pi4j-distribution/target/distro-contents/lib).
But they are part of their modules! So pi4j-device-1.2.jar, pi4j-device-1.2-javadoc.jar and pi4j-device-1.2-sources.jar (yes, they're still referring to Pi4J 1.2 and that should not be changed!) are located in ./target.
Same is valid for pi4j-gpio-extension. The corresponding .jar files can be found in the ./target directory (also here, the .jar files still relate to Pi4J 1.2).
The only drawback (if you can call it like this) is that you have to specify 3 different locations in Netbeans if you want to make your own library and add those 3 Pi4J modules (that is: pi4j-core, pi4j-device and pi4j-gpio-extention) to it. But this is only a small issue compared to the fact that you would not have the modules pi4j-device and pi4j-gpio-extension at all!
There was a heavy dependency on an internal API, both on Pi4J 1.2 as well as Pi4J 1.4. For Pi4J, JDK 8 had to be used and for Pi4J 1.4, JDK 11 had to be used.
Between those two versions the internal API was already changed, so it was not OK to keep on relying on this API. Solution was to get rid of this dependency.
Luckily, in Pi4J 1.4 this dependency has been removed. This can be seen here.
If you check out the very latest Pi4J 1.4 from GitHub, the changes will be in already. But in my version, I still had to do the changes. Once I applied them, all was working fine again without the ugly dependency.
Since there were also a .c and a .h file to be changed, I thought I would be in trouble since I'm not rebuilding .c and .h files in my Maven build. I only rebuild .java files.
But luckily, the changes were already in the provided lib .so file, so changing the .java file was enough for me. Else, I had to try to recompile the native library but therefore I would need a cross compiler and that would mean a lot of hassle...
These are the changes needed in the file LinuxFile.java (part of the pi4j-core module, located in the com.pi4j.io.file package (I repeat here the changes from the link given above, in case the link would disappear one day...)
Remove the "import jdk.internal.misc.SharedSecrets;" line.
Change the getFileDescriptor() method to:
/**
* Gets the real POSIX file descriptor for use by custom jni calls.
*/
private int getFileDescriptor() throws IOException {
final int fd = getPosixFD(getFD());
if(fd < 1)
throw new IOException("failed to get POSIX file descriptor!");
return fd;
}
Add the following line to the end of the LinuxFile class:
protected static native int getPosixFD(FileDescriptor fileDescriptor);
Once this is done, you can recompile the Pi4J 1.4 tree and all should work fine (as said before, the native part is already in the libpi4j-armhf.so file.
There was a time one couldn't build the Pi4J V2 on a Windows machine. Luckily things have changed and I recently experienced it's possible now.
Below are the steps to execute:
If not done so, get the git repository for the Pi4J v2 framework and make sure the local source tree is up2date with the git repository (git pull, you know...)
Once the source files are local, create a branch dev: git branch -b dev
Sync the dev branch with origin/release/2.2 which is the version 2.2.1 as of this writing (the latest stable version of Pi4J v2):
git checkout -B dev origin/release/2.2
You can use GitExtensions to see a graphical view of the Pi4J repository. See image on the right
Once all this is done, go to the repo location on the Windows machine.
Make sure at least maven 3.8.2 is installed on the Windows machine and that the command mvn.exe can be reached from everywhere (add it to the Windows path variable, if needed).
Also make sure Java 11 is installed on the Windows machine.
Those are the maven and Java versions I'm currently working with and all goes fine.
Open a DOS command box at the location where the source files are
Run the command mvn clean install. This will start the complete build and will take a while. If all goes fine, the build should be successful in the end.
The build results can be found in .\pi4j-distribution\target\distro-contents\lib and contains the following .jar and .so files:
pi4j-core-javadoc.jar
pi4j-core-sources.jar
pi4j-core.jar
pi4j-library-linuxfs-aarch64.so
pi4j-library-linuxfs-armhf.so
pi4j-library-linuxfs.jar
pi4j-library-pigpio-aarch64.so
pi4j-library-pigpio-armhf.so
pi4j-library-pigpio.jar
pi4j-plugin-linuxfs.jar
pi4j-plugin-pigpio.jar
pi4j-plugin-raspberrypi.jar
slf4j-api.jar
slf4j-simple.jar
Some of the jar files will be needed to create our NetBeans library. See this section.