### Access External Own Library

This is an example of creating a Java Library and access its class with methods from B4J.
The library enables to change the case of a string to UPPERCASE or lowercase.
Java
Files, Classes and Methods
Java Source Filename: StringCase.java
Java Library Filename: StringCase.jar
Java Package Name: de.rwblinn.stringcase
Java Class Name: StringCase
Class Field: Version
Class Methods: ToUpperCaseEx, ToLowerCaseEx
IMPORTANT:
In the Java source declare the fields and methods as public static final!
Java Source Code StringCase.java with class StringCase
`package de.rwblinn.stringcase;
public class StringCase {
public static final String Version = "20180531";
/**
* Convert String to UPPERCASE
* @param String
* @return String in UpperCase
*/
public static String ToUpperCaseEx(String s){
return s.toUpperCase();
}
/**
* Convert String to lowercase
* @param String
* @return String in lowercase
*/
public static String ToLowerCaseEx(String s){
return s.toLowerCase();
}
}
`
Java Compile Soure to Class
Using a batch file to compile.
The java source is compiled to the java class .../de/rwblinn/stringcase/StringCase.class
`@Echo Off
REM Compile the StringCase.java with the target folder being the packagename de.rwblinn.stringcase
REM The compiler outputs to the folder and class file: de/rwblinn/stringcase/StringCase.class
REM This is the path being used by B4J to gain static access to the class via javaobject method InitializeStatic("de.rwblinn.stringcase.StringCase")
REM 20180531 by rwbLinn
REM Compile - ensure the Java path is added to the environment path
Echo Compiling StringCase.java to StringCase.class
Echo The class file is stored in .../de/rwblinn/stringcase/Stringcase.class
javac StringCase.java -d .
Echo Done
pause
`
Java Create Jar
Using a batch file to create the jar.
The jar packs the class located in de/rwblinn/stringcase/StringCase.class.
The path is important as being accessed by B4J JavaObject.
`@Echo Off
REM Create the jar file StringCase.jar with the class de.rwblinn.stringcase.StringCase
REM This is the path being used by B4J to gain static access to the class via javaobject method InitializeStatic("de.rwblinn.stringcase.StringCase")
REM 20180531 by rwbLinn
Echo Creating the jar file StringCase.jar
jar -cvf StringCase.jar de/rwblinn/stringcase/StringCase.class
Echo Done ... Ensure to copy StringCase.jar to the additional libraries folder.
pause
`
B4J
The B4J project code StringCaseEx.b4j. Read the comments.
`AppType=StandardJava
Build1=Default,de.rwblinn.stringcaseex
Group=Default Group
Library1=jcore
Library2=javaobject
NumberOfFiles=0
NumberOfLibraries=2
NumberOfModules=0
Version=6.3
@EndOfDesignText@
'Non-UI application (console / server application)
Region Project Attributes
#CommandLineArgs:
'Set the external lib parameter - do not forget to set mergelibraries to true
#MergeLibraries: true
'The StringCase.jar library must be stored in the B4J additional library folder
#AdditionalJar: StringCase
End Region
`Sub Process_Globals
Private Const CAPPTITLE As String = "StringCase v20180531"
'Java Object used to access the external library
Private joStringCase As JavaObject
End Sub`
`Sub AppStart (Args() As String)
Log(CAPPTITLE)
Dim TestString As String = "Hello World"
'Init the javaObject with the external lib class packagename.classname as static to use the methods
joStringCase.InitializeStatic("de.rwblinn.stringcase.StringCase")
'get version from ext lib class
Log($"Library version: ${joStringCase.GetField("Version")}"$)
' Log the test string prior conversion
Log($"String: ${TestString}"$)
'Run methods from the ext lib class
Log($"UpperCase: ${joStringCase.RunMethod("ToUpperCaseEx", Array(TestString))}"$)
Log($"LowerCase: ${joStringCase.RunMethod("ToLowerCaseEx", Array(TestString))}"$)
ExitApplication
End Sub`
'Return true to allow the default exceptions handler to handle the uncaught exception.
`Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub`
`

Changed: 2018-06-07 09:16, Category: Libraries, File: T6C214M1553.htm, ID: 1553