I am honestly about to just give up, i’ve tried so many different possibilities, for multiple weeks now, almost a month, of multiple problems.
I am a new-ish programmer, especially with java, but i have a good understanding about java
I am able to create a maven project no problem, i have no problems with the structure of java itself, but i don’t fully understand the pom.xml.
The file compiles just fine, but when i go to start it with java -jar (filename)
, i get the following output;
Exception in thread “main” java.lang.NoClassDefFoundError: com/pi4j/Pi4J at com.pi.rasberri.Main.main(Main.java:16) Caused by: java.lang.ClassNotFoundException: com.pi4j.Pi4J at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) … 1 more
Heres my pom that i have figuratively almost dismembered;
(The long links) <modelVersion>4.0.0</modelVersion> <groupId>com.pi</groupId> <artifactId>Rasberri</artifactId> <version>3.6.1</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.32</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.32</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-raspberrypi</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-pigpio</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId>maven-jar-plugin 3.1.0 true com.pi.rasberri.Main lib/ true true com.pi.rasberri.Main false true
And my code, which is com.pi.rasberri.Main
package com.pi.rasberri; import com.pi4j.Pi4J; import com.pi4j.io.gpio.digital.DigitalOutput; import com.pi4j.io.gpio.digital.DigitalState; import java.util.logging.Level; import java.util.logging.Logger; public class Main { private static final int PIN_LED = 6; public static void main(String[] args) throws Exception{ var pi4j = Pi4J.newAutoContext(); int x = 0; var ledConfig = DigitalOutput.newConfigBuilder(pi4j) .id("led") .name("LED Flasher") .address(PIN_LED) .shutdown(DigitalState.LOW) .initial(DigitalState.LOW) .provider("pigpio-digital-output"); var led = pi4j.create(ledConfig); while(x != 5){ led.high(); sleep(1000); led.low(); sleep(500); x++; } } static void sleep(int z){ try { Thread.sleep(z); } catch (InterruptedException ex) { System.out.println("Thread.sleep went fucky wucky"); } } }
I would appreciate anything that can even direct me somewhere, because i am quite lost at this point, and of course, if i figure out the answer, i will let everyone know! Thanks in advance
**UPDATE**Thank you, tgdavies, the article you linked https://stackoverflow.com/a/574650/17644313 was the answer to that specific problem!**
Just in case anyone else has the same chain of issues as me;
But with that being said, i ran directly into another error;
[main] INFO com.pi4j.Pi4J – New auto context [main] INFO com.pi4j.Pi4J – New context builder [main] INFO com.pi4j.platform.impl.DefaultRuntimePlatforms – adding platform to managed platform map [id=raspberrypi; name=RaspberryPi Platform; priority=5; class=com.pi4j.plugin.raspberrypi.platform.RaspberryPiPlatform] Exception in thread “main” com.pi4j.provider.exception.ProviderNotFoundException: Pi4J provider [pigpio-digital-output] could not be found. Please include this ‘provider’ JAR in the classpath. at com.pi4j.provider.impl.DefaultRuntimeProviders.get(DefaultRuntimeProviders.java:238) at com.pi4j.provider.impl.DefaultProviders.get(DefaultProviders.java:147) at com.pi4j.provider.Providers.get(Providers.java:253) at com.pi4j.context.Context.create(Context.java:316) at com.pi4j.internal.IOCreator.create(IOCreator.java:58) at com.pi4j.internal.IOCreator.create(IOCreator.java:96) at com.pi4j.internal.IOCreator.create(IOCreator.java:176) at com.pi.rasberri.Main.main(Main.java:27)
I cleaned the pom.xml a littlebit;
<dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.32</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.32</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-raspberrypi</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-pigpio</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <plugins> <!--Package all libraries classes into one runnable jar --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <mainClass>com.pi.rasberri.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> </project>
At this point, i know of atleast 2 possibilities,
- The code just needs to be run on the raspberry pi
- I have entered the classpaths incorrectly
But i honestly dont really know as to where to start troubleshooting I’ve tried a few things, one of which being to try another method of compiling the files, but the other method just resulted in a even longer string of errors.
What should i try next? Thanks in advance! And thanks to anyone who has suggested troubleshooting steps thus far!
Oh, and the target is to hopefully just create a singular jar file to execute on the raspberry pi
update
I tried to run it on the Raspberri pi, and it resulted in the same error so it’s not that
Advertisement
Answer
Thanks tgdavies, MadProgrammer, and khmarbaise for the answers, the fix was to create a fat jar, which is basically a jar file that contains all the dependencies in one file, example can be found in the original question/comments
UPDATE!!
Pi4j has posted instructions for the V2 fat jar on their website! I tried it and it works perfectly now
https://pi4j.com/getting-started/minimal-example-application-fatjar/