Pi4J / pi4j-v2

Pi4J Version 2.0
Apache License 2.0
281 stars 62 forks source link

Throwing error while fatJaring using gradle #237

Open Reqeique opened 2 years ago

Reqeique commented 2 years ago

I was currently experimenting with pi4j but when I start to fat Jar it, it kept throwing exception my first attempt was to utilize shadowJar, which wasn't able to jar all the necessary libraries resulting lib wasn't found exception so I put all the necessary jar files manually but gave Method [create([class java.lang.Integer])] could not be resolved in provider [pigpio-digital-output(com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalOutputProviderImpl)]; Overridden instances of 'create()' are not supported unless defined as default methods in the provider interface following that I tried my third attempts which was to utilize built in jaring method using the following code

task("fatJar", type = Jar::class){

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes(mapOf("Main-Class" to "myClass"))
    }
    from(configurations.runtimeClasspath.get().map{ if
            (it.isDirectory) it else zipTree(it) })
    with(tasks["jar"] as CopySpec)
}

but throws the same execption no luck! Note: the code i used was from https://pi4j.com/documentation/io-examples/digital-output/ which was then converted to kotlin

taartspi commented 2 years ago

One thing, use this as the example of code. I believe once your present code creates and runs through the fat, it will not change the LED. https://github.com/Pi4J/pi4j-example-minimal

I created a clone of this example successfully ran it.

FAT used info from https://pi4j.com/documentation/building/fat-jar/ and example https://github.com/Pi4J/pi4j-example-fatjar to copy the needed edits to the pom file.

I edited the pom file to add the FAT specific details . Was able to: mvn clean package, cd into target/distribution and sudo java -jar pi4j-example-fatjar.jar

Give me a minute and I will attach my pom

hope this help. Tom

taartspi commented 2 years ago

pom.txt

taartspi commented 2 years ago

I could see you prefer gradle to build. I used the example testcase from above and added gradle to it. I see an error. I normally use maven so all this is a matter of digging and I have not found an answer. I do see that running under gradle build there are fewer providers loaded.

Maven these output providers. ├─DIGITAL_OUTPUT: [3] │ ├─PROVIDER: "PiGpio Digital Output (GPIO) Provider" {pigpio-digital-output} {com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalOutputProviderImpl} │ ├─PROVIDER: "LinuxFS Digital Output (GPIO) Provider" {linuxfs-digital-output} {com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalOutputProviderImpl} │ └─PROVIDER: "RaspberryPi Digital Output (GPIO) Provider" {raspberrypi-digital-output} {com.pi4j.plugin.raspberrypi.provider.gpio.digital.RpiDigitalOutputProviderImpl} ├─I

Using gradle only └─PROVIDER: "RaspberryPi Digital Output (GPIO) Provider" {raspberrypi-digital-output} {com.pi4j.plugin.raspberrypi.provider.gpio.digital.RpiDigitalOutputProviderImpl}

My error is Exception in thread "main" com.pi4j.provider.exception.ProviderNotFoundException: Pi4J provider [pigpio-digital-output] c

Was this your initial error when you said a 'lib' was not found ?

Reqeique commented 2 years ago

@taartspi that was absolutely the error that I faced while fatJaring for the first time but was resolved easily(not considering the last exception)

RLEO94 commented 2 years ago

Hi did you solve this problem?

I got the same exception. If I run my code on my computer in Intellij IDEA with the Embedded Linux JVM plugin, then the code works well (GPIO Out turns HIGH and Input is detected, ecc..).

If I create a fatjar with gradle, copy the file to my RPI4 and execute it with java -jar, then I got the same exception (Provider not found). On the webside is described how to create a fat jar with Maven: https://pi4j.com/documentation/building/fat-jar/.
So I try now to use this plugin: https://github.com/dkorotych/gradle-maven-exec-plugin with the goal to execute the described maven commands/plugins through gradle, but I'm still struggling

Reqeique commented 2 years ago

@RLEO94 for Provider not found you could easily tweak it by modifying the fatjar to include the the library that wasn't found manually by using tools like win-rar or creating custom jaRing method as above(that was taken from here ) to include all dependencies

overall can't guarantee wouldn't throw Method [create([class java.lang.Integer])] could not be resolved in provider

RLEO94 commented 2 years ago

Hi Thanks for your help. I will give it a try :)

charbelmkh commented 1 year ago

Hey @Reqeique, I'm not sure if you were able to solve this issue, but I managed to solve it by checking out pi4j-kotlin & using shadow plugin & configuring the below gradle task

tasks.named("shadowJar", ShadowJar::class.java) {
    mergeServiceFiles()
    manifest.attributes["Main-Class"] = "MinimalExampleKt"
}
bennymeg commented 5 months ago

This is worth including in the docs alongside the maven Uber Jar document. More information can be found in shadow: merging-service-descriptor-files.