### B4J Library create using Java

This tutorial creates a B4J Library using Java 8 with Eclipse Kepler.
The library provides additional string functions and is called jRLStrings.
Create a Project Folder in the selected Eclipse workspace
Folder: jRLStrings
Create a new Java Project in Eclipse
• Start Exclipse, select your workspace
• Menu File > New > Java Project
• Project Name: jRLStrings
• Click Next
• Default Output Folder: jRLStrings/bin
• Click Finish
Amend Build Path
• In the package explorer, select the new project and right click
• Select Build Path > Configure Path
• Select Add external Jar
• Add jCore from the B4J Library folder (f.e. c:\Program Files (x86)\Anywhere Software\B4J\Libraries\jCore.jar)
Create a new Class
In the package explorer:
• Select jRLStrings > src
• Right mouse click
• New > Class > Class Name: RLStrings
• Select in the package explorer, the entry under source and rename from jRLStrings to com.rwblinn.rlstrings
Add BA reference to the class RLStrings
Add first in the source of the jRLStrings Class:
import anywheresoftware.b4a.BA;
// In case dependencies, add @BA.DependsOn(values={"bmp"})
@BA.DesignerName("Build 20140101")
@BA.Version(0.10F)
@BA.Author("Robert Linn")
@BA.ShortName("RLStrings")
Then select import anywheresoftware.b4a.BA;
Right click and fix project setup, a link will be added to jCore.
Note: Obsolete step if jCore.jar has been added to the build path
Add first string function to the class
/**
* Checks if a string is empty. A null value is also handled.
* @param string = The string to check
* @return True = String is empty else False
* */
public boolean isEmtpy(String str)
{
return "".equals(str);
}
and save CTRL-S. Check the tab Problems for errors
Create the Library XML file
Various steps are required, starting with
Menu Project > Generate Javadoc >
Dialog Select Types for Javadoc Generation
Javadoc command:
Locate your javadoc.exe (Example C:\Program Files (x86)\Java\jdk8\bin\javadoc.exe)
Select Types:
Select jRLStrings (Ensure to activate all the classes to be exported).
Create Javadoc for members with visibility:
Select PUBLIC
Use custom Doclet:
Doclet name: BADoclet
(Note: be exact on the case!!!)
Doclet class path: c:\Daten\b4\b4j\doclet
(Note: download first the BADoclet from B4A website)
Note: Do not forget to select the java project jRLString as mentioned earlier!
Click Next
Dialog Configure Javadoc Arguments
Extra Javadoc options: -b4atarget "c:\Daten\b4\b4j\libraries\jRLStrings.xml"
Note: The target given here, is the authors B4J Additional Libraries folder.
Do not forget the minus sign prior b4atarget
JRE source compatibility:
1.7
After clicking finish the log shows:
Loading source files for package com.rwblinn.rlstrings...
Constructing Javadoc information...
[-doclet, BADoclet]
[-docletpath, c:\Daten\b4\b4j\doclet]
[-doclet, BADoclet]
[-docletpath, c:\Daten\b4\b4j\doclet]
[-sourcepath, C:\Daten\b4\b4j\opensource\rolibs\jRLStrings\src]
[-classpath, C:\Program Files (x86)\Anywhere Software\B4J\Libraries\jCore.jar]
[-public]
[-b4atarget, c:\Daten\b4\b4j\libraries\jRLStrings.xml]
starting....
Working with class: com.rwblinn.rlstrings.RLStrings
finish: c:\Daten\b4\b4j\libraries\jRLStrings.xml
Build the jar file
Select Menu File > Export > Java
Dialog Export Resources into a Jar file
Select Java >Jar File
click next
Dialog JAR File Specification
Select the resources to export:
Tick jRLUtils and ensure srv and doc are ticked as well
Tick Export generated class files and resources
Export destination:
c:\Daten\b4\b4j\libraries\jRLStrings.jar
Options:
• Tick Compress
• Tick Add directory entries
Click finish.
Go to the folder where the jar and the xml are located.
In this case as mentioned the additional libraries folder c:\Daten\b4\b4j\libraries.
The advantage of creating directly in the additional library folder, are
• no additional copying is required
• the library is shown in the B4J IDE Library Manager
• when making changes, only action is to refresh the B4J IDE Library Manager
Test the library
Create a B4J Sample project.
Example of testing this library with the IsEmpty function, using a textfield and a button:
' Test jRLString Library Function isEmpty
`Sub btnIsEmtpy_Action
Dim rls As jRLStrings
Log("textStringIsEmpty: " & rls.isEmtpy(txtString.Text))
End Sub`
Making changes to the Library and test in B4J
Simple steps:
• 1. Update the code
• 2. Increment the version and the date for the BA qualifier, like @BA.Version(0.10F)
• 3. If no change in Javadoc required, the Export as JAR and refresh the B4J IDE Library Manager (right click and choose Refresh)
• 4. If change in javadoc because doc changes made, then create Javadoc first before step 2
• 5. In the B4J IDE Library Manager refresh the list. The version for the Lib should be incremented (if 2. used)
Note on using JavaDoc directives
These are not supported by the BADocLet (Status 20140724).
But if you want to use, these are some of the most common:
• @param [argument name] [argument description] - describes an argument of method or constructor.
• @return [description of return] - describes data returned by method (unnecessary for constructors and void methods).
• @exception [exception thrown] [exception description] - describes exception thrown by method.
• @throws [exception thrown] [exception description] - same as @exception.
To show code example make use of the `` tags.
Use the Simple Library Compiler (SLC)
See under "Build Libraries without Eclipse"for detailed info.
More Information:
http://www.b4x.com/android/forum/threads/tool-simple-library-compiler-build-libraries-without-eclipse.29918/
Batch to compile the library worked in this example. The output is in the additional library folder.
Echo Off
Echo SLC Compile
Echo For Library jRLUtils in folder c:\Daten\b4\b4j\opensource\rolibs\jRLUtils\
Echo ---------------------------------------------------------------------------
Rem
Rem slccompiler library projectfolder
c:\Daten\b4j\ztools\SimpleLibraryCompiler\B4J_LibraryCompiler jRLUtils c:\Daten\b4\b4j\opensource\rolibs\jRLUtils\
Rem
Pause
Additional Links with information
B4J Forum: [java] Libraries and multithreading
http://www.b4x.com/android/forum/threads/java-libraries-and-multithreading.12697/#content
Wrapping
When wrapping an existing library, ensure to set in the Java Build Path:
Tab Libraries:
Add external jars, the jars to be included, like jcore.jar and the to be wrapped library
Tab Order and Export:
Select the to be wrapped library - this ensures the lib reference is included in the wrapped jar
In the source code, set dependson with the external jar.
Example using external jar file bmp.jar located in the additional libraries folder.
@BA.DependsOn(values={"bmp"})

Changed: , Category: Libraries, File: T6C214M1556.htm, ID: 1556