Are there the examples about using #AdditionalJar Vs using JavaObject with javacode in the same case for learning comparison coding?
I don't think you can find easily such example on this forum.
Reason is if you use the
same Java code instead of adding a third party library, it is like reinventing the wheel or extract all the
code inside the third party library and paste them all in B4X IDE.
Why someone wants to do it?
how to convert javacode to jar file?
There are some ways to do it.
- Add a new class and put in your Java code. Use JavaObject to call the public Java functions. Use the IDE menu "Compile To Library (Alt + 5)" and you get a B4A or B4J library (.jar and .xml) in your additional libraries folder.
- Use Simple Library Compiler to compile your Java code.
- Create the library with Eclipse or IntelliJ.
- The jar library created from method 1 and 2 above will be listed on the IDE Libraries Manager tab.
- The jar library created from method 3 or downloaded from somewhere else like maven repository will not listed on the IDE Libraries Manager tab and you need to reference it using the #AdditionalJar keyword.
You need to understand when to use #AdditionalJar keyword.
B4A and B4J already built-in with many core libraries by default.
- When the inline Java code you added within #If Java only uses the core libraries such as java.util or java.io then you don't need to use #AdditionalJar. This is the case in the pyramid * example you asked in the link.
- When you want to make use of more advanced Java code like when you are creating a wrapper as a new library for B4A or B4J then you may need to include (or import) the third party library that you can download from maven or other library providers.
For example I want to create a ImageScaler library.
The
Java source code I found on Internet has an import to a library to
imgscalr.jar library.
#If Java
import java.io.File;
import java.io.IOException;
import org.imgscalr.Scalr;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
On the Github page, go to Releases (right panel of the page) and click on Tags. You may found the latest version of the compiled jar.
Sometimes you only found the zip files contains the project source code or src folder. This mean you need to compile the library yourself.
Before that, try to google search for the library where the compiled jar file may be available and hosted on
Maven Central repository.
From the source code published on Internet tutorial like baeldung, you can see something like this:
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
So the keyword you need to search is the package name inside the groupId tag which is org.imgscalr.
Next, in order for you to create the wrapper, you need to read the project documentation or example source code on Internet.
You don't need to understand much about Java which I am not. Things to take note is you can use the public functions available and the matching data type when use in B4X.
For example, you got a Java code like following:
public static Dimension getSize (String filename)
{
try {
BufferedImage bimg = ImageIO.read(new File(filename));
int width = bimg.getWidth();
int height = bimg.getHeight();
return new Dimension(width, height);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Pay attention only on first line.
The function name getSize, which accepts parameter in String data type.
When we want to call this function in B4X using JavaObject, we write it as follow:
Dim dimensions As JavaObject = jo.RunMethodJO("getSize", Array(ImageFile))
We pass the name of the function in first parameter i.e "getSize" and the value of String data type using Array() as the second parameter of RunMethodJO method.
To learn more about JavaObject, read
The purpose of JavaObject library is similar to the purpose of Reflection library. Both libraries allow you to directly call Java APIs based on Java reflection features. JavaObject design is different than Reflection library and in most cases simpler to use. However JavaObject doesn't replace...
www.b4x.com
To learn more about using third party library using #AdditionalJars, read
The #AdditionalJar module attribute (introduced in B4A v3.80) allows us to reference external jars. With the help of JavaObject it is now possible to integrate third party jars without a wrapper. This solution is good for "simple" libraries. If the API is complicated with many interfaces then...
www.b4x.com