B4J Question How to use an Additional JAR

Brian Dean

Well-Known Member
Licensed User
Longtime User
This is my first attempt to use an additional jar. I am using what we call in the UK a "monkey see; monkey do" approach - trying to copy what others seem to have done, not necessarily understanding what I am doing. Helped by posts on this forum I have constructed a test using the zip4J library that I have found at this link. Here is my complete program :

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #AdditionalJar: zip4j-2.11.1.jar
#End Region

Sub Process_Globals
    Private MainForm As Form
    Private Button1 As B4XView            
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub

Sub Button1_Click
    zip("E:/Temp/Waffle.jpg", "E:/Temp/Waffle.zip")            ' Test sample
End Sub

Public Sub zip(filename As String, zipFile As String)
    Dim zipper As JavaObject
    Dim constants As JavaObject
    zipper.InitializeNewInstance("net.lingala.zip4j.core.ZipFile", Array(zipFile))
    constants.InitializeStatic("net.lingala.zip4j.util.Zip4jConstants")   
    zipper.RunMethod("addFile", Array(filename))   
End Sub

I am getting the error :
java.lang.ClassNotFoundException: net.lingala$zip4j$core$ZipFile

What have I missed?
 
Solution
net.lingala.zip4j
You are still missing the last part :
net.lingala.zip4j.ZipFile
what you had extra was the core part

you usually use the last line on this image:
1673907463083.png


Array As String(zipFile)
dont use array as string, this is actually creating a constructor and passing parameters it is not passing the parameters directly so even if the method requires an String, you want to use a simple Array

if it still fails, share the .jar file you are using, you may be even using a different version

Daestrum

Expert
Licensed User
Longtime User
It looks like
B4X:
zipper.InitializeNewInstance("net.lingala.zip4j.core.ZipFile", Array(zipFile))
should maybe be
B4X:
zipper.InitializeNewInstance("net.lingala.zip4j.ZipFile", Array As String(zipFile))

I would think it will pull in the constants itself so no need for the IntializeStatic line


But.... I could be wrong.

I did edit it after you must have looked at it as I realized I had left off the Zipfile at the end of the line.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Thanks for your suggestion. I changed line 25 in the original post to this :
B4X:
    zipper.InitializeNewInstance("net.lingala.zip4j", Array As String(zipFile))

The error that I get now is ...
B4X:
java.lang.ClassNotFoundException: net$lingala$zip4j
. . . so not really further forward.

Edit : Sorry - I didn't follow your instructions correctly. My mistake.
 
Last edited:
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
net.lingala.zip4j
You are still missing the last part :
net.lingala.zip4j.ZipFile
what you had extra was the core part

you usually use the last line on this image:
1673907463083.png


Array As String(zipFile)
dont use array as string, this is actually creating a constructor and passing parameters it is not passing the parameters directly so even if the method requires an String, you want to use a simple Array

if it still fails, share the .jar file you are using, you may be even using a different version
 
Upvote 0
Solution
Top