weliem / blessed-bluez

BLE library using Java and Bluez
MIT License
76 stars 21 forks source link

Integrating blessed-bluez into Desktop BLE Application #10

Closed L4m4L closed 2 years ago

L4m4L commented 2 years ago

Hello Martijn,

Firstly thank-you for your work developing this library, it looks to be exactly what I am after for in my project. I am hoping you could help me integrate this library into an existing java project in Eclipse. I am trying to build a plugin for an existing open-source java application, to send data from this application over BLE to an external device.

I have included the blessed-0.40.jar as a "Referenced Library", and I have also included the appropriate dependencies (logback-core/classic and blessed-bluez) in my pom.xml file. Doing this allows me to compile/build my project successfully without any errors.

Where I am running into errors is when I run the open-source application with my plugin in the "plugins" directory. Initially I was encountering "ClassNotFoundException: com.welie.blessed.BluetoothPeripheralCallback" which I resolved by placing the blessed-40.jar into the plugins directory. Re-running, I continue to get similar errors, which I can only seem to fix by putting more and more jars into the plugins folder.

I now have:

Apologies for the background but what I am trying to understand is - how does one integrate this library with an existing java project? Should I be compiling in a certain way to include all the dependencies/classes in my output jar? Or is there another way to "point" the application to the required files at run-time?

Thanks very much

weliem commented 2 years ago

I don't have any experience with Eclipse, but I think you should put everything in the pom.xml file. Then all dependencies should be loaded automatically.

Try adding something like this:

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.github.hypfvieh/dbus-java -->
    <dependency>
        <groupId>com.github.hypfvieh</groupId>
        <artifactId>dbus-java</artifactId>
        <version>3.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>

.....so if you import dbus-java you would get all the other ones as well....

L4m4L commented 2 years ago

Hey @weliem, thanks for your response.

I tried changing my pom file to include those two dependencies, but the program still fails to load the plugin based on it being unable to find the required classes at runtime.

Error: org.herac.tuxguitar.util.plugin.TGPluginException: An error ocurred when trying to connect plugin at org.herac.tuxguitar.util.plugin.TGPluginManager.connectPlugin(TGPluginManager.java:92) at org.herac.tuxguitar.util.plugin.TGPluginManager.connectEnabled(TGPluginManager.java:65) at org.herac.tuxguitar.app.TuxGuitar.startUIContext(TuxGuitar.java:139) at org.herac.tuxguitar.app.TuxGuitar.access$100(TuxGuitar.java:67) at org.herac.tuxguitar.app.TuxGuitar$1$1.run(TuxGuitar.java:122) at org.herac.tuxguitar.app.synchronizer.TGSynchronizerControllerImpl$1$1.run(TGSynchronizerControllerImpl.java:29) at org.eclipse.swt.widgets.RunnableLock.run(Unknown Source) at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Unknown Source) at org.eclipse.swt.widgets.Display.runAsyncMessages(Unknown Source) at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source) at org.herac.tuxguitar.ui.swt.SWTApplication.start(SWTApplication.java:64) at org.herac.tuxguitar.app.TuxGuitar.createUIContext(TuxGuitar.java:115) at org.herac.tuxguitar.app.TuxGuitar.createApplication(TuxGuitar.java:100) at org.herac.tuxguitar.app.TGMainSingleton.launchTuxGuitar(TGMainSingleton.java:57) at org.herac.tuxguitar.app.TGMainSingleton.launchSingleton(TGMainSingleton.java:45) at org.herac.tuxguitar.app.TGMainSingleton.main(TGMainSingleton.java:35) Caused by: java.lang.NoClassDefFoundError: com/welie/blessed/BluetoothPeripheralCallback at org.herac.tuxguitar.example.redrawlistener.RedrawListenerExample.<init>(RedrawListenerExample.java:28) at org.herac.tuxguitar.example.redrawlistener.PluginExample.connect(PluginExample.java:22) at org.herac.tuxguitar.util.plugin.TGPluginManager.connectPlugin(TGPluginManager.java:88) ... 15 more Caused by: java.lang.ClassNotFoundException: com.welie.blessed.BluetoothPeripheralCallback at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ... 18 more

Even though I am including com.github.weliem.blessed-bluez in my pom dependencies and Referenced Libraries in eclipse...

Any further help would be appreciated. Thankyou

EDIT: I managed to run without these error by compiling as a "fat JAR" with all dependencies included as per: https://jenkov.com/tutorials/maven/maven-build-fat-jar.html. I don't think this is the best option for me, as it increases my .jar file size from 10kb to 4.8 MB !! Is there another way that you would recommend to go around this?