RaspiTechniker
Posts: 6
Joined: Sat Jan 16, 2021 9:02 am

Spring Boot and Pi4J Lib with MCP23017

Sat Jan 16, 2021 11:46 am

Hello everybody,
I've experimented a bit with the Raspi and Boot Spring in the last few days.
Using the PI4J Lib worked very well for simple things. However. Now I have a problem when I explicitly want to use the MCP23017 to control relays etc. via the I2C bus.
Addressing individual pins in a project beforehand worked.
I would be very grateful for tips.
As an IDE I use Intellij and assemble the jar file with Maven.

I2C is running on my PI:
sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

I get the following error message when I want to create the MCP23017 provider:

Code: Select all

JVM running for 19.744)
<------ Start GPIO ---------->
<------ create MCP23017 provider ---------->
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.pi4j.io.file.LinuxFile (jar:file:/home/pi/Downloads/RelaisSteuerungBackend-0.0.4-SNAPSHOT.jar!/BOOT-INF/lib/pi4j-core-1 .3-SNAPSHOT.jar!/) to field java.nio.Buffer.address
WARNING: Please consider reporting this to the maintainers of com.pi4j.io.file.LinuxFile
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.NoClassDefFoundError: sun/misc/SharedSecrets
at com.pi4j.io.file.LinuxFile.getFileDescriptor(LinuxFile.java:215)
at com.pi4j.io.file.LinuxFile.ioctl(LinuxFile.java:103)
at com.pi4j.io.i2c.impl.I2CBusImpl.selectBusSlave(I2CBusImpl.java:291)
at com.pi4j.io.i2c.impl.I2CBusImpl.runBusLockedDeviceAction(I2CBusImpl.java:258)
at com.pi4j.io.i2c.impl.I2CBusImpl.readByte(I2CBusImpl.java:153)
at com.pi4j.io.i2c.impl.I2CDeviceImpl.read(I2CDeviceImpl.java:205)
at com.pi4j.gpio.extension.mcp.MCP23017GpioProvider.<init>(MCP23017GpioProvider.java:129)
at com.pi4j.gpio.extension.mcp.MCP23017GpioProvider.<init>(MCP23017GpioProvider.java:113)
at com.pi4j.gpio.extension.mcp.MCP23017GpioProvider.<init>(MCP23017GpioProvider.java:108)
at com.example.RelaisSteuerungBackend.controller.InitGpios.initGpioPins(InitGpios.java:23)
at com.example.RelaisSteuerungBackend.RelaisSteuerungBackendApplication.main(RelaisSteuerungBackendApplication.java:18)
... 8 more

Caused by: java.lang.ClassNotFoundException: sun.misc.SharedSecrets
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 19 more
My JAVA-Code:

Code: Select all

package com.example.RelaisSteuerungBackend.controller;

import com.pi4j.gpio.extension.mcp.MCP23017GpioProvider;
import com.pi4j.gpio.extension.mcp.MCP23017Pin;
import com.pi4j.io.gpio.*;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;

import java.io.IOException;

public class InitGpios {

    public static void initGpioPins() throws IOException, I2CFactory.UnsupportedBusNumberException {

        System.out.println("<------ Start GPIO ---------->");

        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        System.out.println("<------ create MCP23017 provider ---------->");
        // create custom MCP23017 GPIO provider
        final MCP23017GpioProvider provider = new MCP23017GpioProvider(I2CBus.BUS_1, 0x27);

        System.out.println("<------ Init Pins ---------->");
        GpioPinDigitalOutput pinB1 = gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B0, PinState.LOW);
        System.out.println("<------ GPIO_B0 State:" + pinB1.getState().toString() +  " ---------->");


        GpioPinDigitalOutput pinB2 = gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B1, PinState.LOW);
        System.out.println("<------ GPIO_B1 State:" + pinB2.getState().toString() +  " ---------->");

        GpioPinDigitalOutput pinB3 = gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B2, PinState.LOW);
        System.out.println("<------ GPIO_B2 State:" + pinB3.getState().toString() +  " ---------->");

    }
}

I use PI4J Snapshot1.3 as LIB, which I installed locally.
See this about the dependencies in my POM:

Code: Select all


<properties>
		<java.version>11</java.version>
	</properties>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>1.3-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/pi4j-1.3-SNAPSHOT/pi4j-core.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-device</artifactId>
            <version>1.3-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/pi4j-1.3-SNAPSHOT/pi4j-device.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-gpio-extension</artifactId>
            <version>1.3-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/pi4j-1.3-SNAPSHOT/pi4j-gpio-extension.jar</systemPath>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration><includeSystemScope>true</includeSystemScope></configuration>
            </plugin>
        </plugins>
    </build>
When i deactivate the Spring Boot Application, i have the same problem:

Code: Select all

<------ Start GPIO ---------->
<------ create MCP23017 provider ---------->
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.pi4j.io.file.LinuxFile (jar:file:/home/pi/Downloads/RelaisSteuerungBackend-0.0.5-SNAPSHOT.jar!/BOOT-INF/lib/pi4j-core-1.3-SNAPSHOT.jar!/) to field java.nio.Buffer.address
WARNING: Please consider reporting this to the maintainers of com.pi4j.io.file.LinuxFile
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.NoClassDefFoundError: sun/misc/SharedSecrets
        at com.pi4j.io.file.LinuxFile.getFileDescriptor(LinuxFile.java:215)
        at com.pi4j.io.file.LinuxFile.ioctl(LinuxFile.java:103)
        at com.pi4j.io.i2c.impl.I2CBusImpl.selectBusSlave(I2CBusImpl.java:291)
        at com.pi4j.io.i2c.impl.I2CBusImpl.runBusLockedDeviceAction(I2CBusImpl.java:258)
        at com.pi4j.io.i2c.impl.I2CBusImpl.readByte(I2CBusImpl.java:153)
        at com.pi4j.io.i2c.impl.I2CDeviceImpl.read(I2CDeviceImpl.java:205)
        at com.pi4j.gpio.extension.mcp.MCP23017GpioProvider.<init>(MCP23017GpioProvider.java:129)
        at com.pi4j.gpio.extension.mcp.MCP23017GpioProvider.<init>(MCP23017GpioProvider.java:113)
        at com.pi4j.gpio.extension.mcp.MCP23017GpioProvider.<init>(MCP23017GpioProvider.java:108)
        at com.example.RelaisSteuerungBackend.controller.InitGpios.initGpioPins(InitGpios.java:23)
        at com.example.RelaisSteuerungBackend.RelaisSteuerungBackendApplication.main(RelaisSteuerungBackendApplication.java:18)
        ... 8 more
Caused by: java.lang.ClassNotFoundException: sun.misc.SharedSecrets
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 19 more

Could it be because of the JAVA 11? Is there an alternative that I can freely use to use PI4J?


I am very happy to receive comments and help.
Thank you.

RaspiTechniker
Posts: 6
Joined: Sat Jan 16, 2021 9:02 am

Re: Spring Boot and Pi4J Lib with MCP23017

Sat Jan 16, 2021 7:54 pm

I have the solution, i need Java 8. I have switch the version from Spring Boot and Intellij and now its works.
Thank you :)

FrankDelporte
Posts: 6
Joined: Mon Dec 09, 2019 9:13 pm

Re: Spring Boot and Pi4J Lib with MCP23017

Sun Jan 17, 2021 8:23 am

Indeed Pi4J V1.2 and V1.3 use Java 8, next versions will allow you to use newer ones.
V2 (early access, not released yet) is aimed at Java 11+ - https://v2.pi4j.com/

If you want to get started with the V2 version, you can find a full example described on https://v2.pi4j.com/getting-started/min ... pplication

Return to “Java”