Android Question Using #Additionaljar Vs using JavaObject with javacode

Theera

Expert
Licensed User
Longtime User
Last edited:

Theera

Expert
Licensed User
Longtime User
This is not related to B4X.

If you want to learn how to compile java code i suggest to do a Googlesearch. There are different ways. Using Eclipse, IntelliJ...

compile java file
I asked for someone to provide additional knowledge (Google translate). I just know only B4X and a little Vb6.0
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I you want to use #AdditionalJar the you must have the JAR in your additional library folder. The you declare it with #AdditionalJar in your B4x project. Then you can use JavaObject to interface with the JAR. You need to be able to view the contents of the JAR so that you know how to interface with the classes and methods in the JAR.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
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.
  1. 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.
  2. Use Simple Library Compiler to compile your Java code.
  3. 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.
Java:
#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:
XML:
<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:
Java:
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:
B4X:
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
To learn more about using third party library using #AdditionalJars, read
 
Last edited:
Upvote 0
Top