### Access External Library 1

The #AdditionalJar module attribute allows to reference external jars.
With the help of JavaObjects (requires the JavaObject library) it is possible to integrate third party jars without a wrapper.
Get external Jar information
To obtain insights in Jar, tools are available like JD-Gui or JD Java Decompiler.
Quick Intro
Based upon snippets from the B4J HowTo Example Project previous mentioned, using the external library jssc.jar.
jSSC is a simple serial connector.
Define in the Project Attributes the additional Jar (without .jar extension)
#AdditionalJar: jssc
`Sub Process_Globals
Private lvwPorts As ListView ' Lists the COM ports found
Private lvwPortInfo As ListView ' Shows some information about the selected COM port
End Sub`
' Get all the COM ports
' Defined in jssc.jar as:
' package jssc; Public class SerialPortList; public static String[] getPortNames()
' To run the method getPortNames, create a JavaObject, access the class first to then run the method getPortNames
`Sub listPorts
' Define the JavaObject to access the class
Dim joPorts As JavaObject
' Init the JavaObject = Packagename.Class
joPorts.InitializeStatic("jssc.SerialPortList")
' Get the portnames by running the method getPortNames
Dim ports() As String = joPorts.RunMethod("getPortNames", Null)
' Add the array of string holding the portnames to the listview
lvwPorts.Items.Clear
If ports.Length > 0 Then
lvwPorts.Items.AddAll(ports)
Else
lvwPorts.Items.Add("No COM Ports found.")
End If
End Sub`
' get information for a port
' Defined in jssc.jar as:
' package jssc; Public class SerialPort; public SerialPort(String portName)
' To run methods from this class, a new instance has to be defined with the port as parameter
' Then methods can be called
`Sub lvwPorts_SelectedIndexChanged(Index As Int)
If lvwPorts.SelectedIndex < 0 Then Return
Try
Dim port As String = lvwPorts.SelectedItem
If port.Length = 0 Then Return
' Define the JavaObject to access the class
Dim joPorts As JavaObject
' Init the JavaObject as a new instance with the selected port as parameter
' Packagename.Class with Port (String) as Parameter
joPorts.InitializeNewInstance("jssc.SerialPort", Array As Object(port))
lvwPortInfo.Items.Clear
lvwPortInfo.Items.Add("Portname: " & joPorts.RunMethod("getPortName", Null))
lvwPortInfo.Items.Add("Open: " & joPorts.RunMethod("isOpened", Null))
lvwPortInfo.Items.Add("Linestatus: " & joPorts.RunMethod("getLinesStatus", Null))
' Etc
Catch
lvwPortInfo.Items.Add(LastException.Message)
End Try
End Sub`

Changed: 2018-06-07 09:14, Category: Libraries, File: T6C214M1552.htm, ID: 1552