So I built a simple java program with dependencies to call from within B4J using the JavaObject. The jar files I generated (I tried several different options) all worked in standalone mode (java -jar ReportWrapper.jar) but failed in B4J. I was hoping maybe a "Fat" jar (where all dependencies are copied into the jar - not the one were dependencies are extracted into the jar file ... but I did try that as well) but that did not work.
I can get one of them to work in B4j in Debug mode but I cannot get it to work in Release mode. So, I am looking for advice on how to configure and deploy my simple java program (with dependencies on other jars) so that it will work with a deployed B4J app. As you look at my example, please note that I know there is a jJasperReports Library but I did encounter some problems with it (that are still being worked) and I will need to write custom java code for some future features that I plan to deploy so this will be a good learning experience for me.
Here is more information. What I am hoping for is some guidance and/or requirements for what it takes to build a B4X compatible Jar file when there are dependencies on other jar files. I will be happy to provide even more on request. I am not really asking anybody to troubleshoot my attempt but that would be welcome if it will help me understand how to do this properly.
First, my custom Java Code:
Next, my B4X call:
Now, the error I encounter in B4J Release mode (Debug mode works just fine!):
I can post more of the stack trace if you need it.
I can get one of them to work in B4j in Debug mode but I cannot get it to work in Release mode. So, I am looking for advice on how to configure and deploy my simple java program (with dependencies on other jars) so that it will work with a deployed B4J app. As you look at my example, please note that I know there is a jJasperReports Library but I did encounter some problems with it (that are still being worked) and I will need to write custom java code for some future features that I plan to deploy so this will be a good learning experience for me.
Here is more information. What I am hoping for is some guidance and/or requirements for what it takes to build a B4X compatible Jar file when there are dependencies on other jar files. I will be happy to provide even more on request. I am not really asking anybody to troubleshoot my attempt but that would be welcome if it will help me understand how to do this properly.
First, my custom Java Code:
Java:
package net.nmcollector.reportwrapper;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.JasperViewer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
public class ReportWrapper {
public static void main(String[] args) {
System.out.println("Hello from net.nmcollector.cpReports main");
for (int i = 0; i < args.length; i++) {
System.out.println(("main - value of args( " + i + "): " + args[i]) );
}
String[] str = new String[5];
if (args.length==0){
str[0] = "C:\\Users\\cjpry\\AppData\\Roaming\\nmcollector\\nmcswDB.sqlite";
str[1] = "C:\\Users\\cjpry\\AppData\\Roaming\\nmcollector\\Reports\\";
str[2] = "Inventory.jasper";
str[3] = "1";
str[4] = "1";
} else {
str = args;
}
int returnCode = openReport(str);
System.out.println("call to openReport returnCode = " + returnCode);
}
public static int openReport(String[] args) {
int returnCode = -1;
System.out.println("Hello from net.nmcollector.cpReports openReport");
for (int i = 0; i < args.length; i++) {
System.out.println(("openReport value of args( " + i + "): " + args[i]) );
}
String jasperReport = args[1] + args[2];
System.out.println("net.nmcollector.cpReports openReport jasperReport = " + jasperReport);
String datasourceConnectionString = args[0];
System.out.println("net.nmcollector.cpReports openReport datasourceConnectionString = " + datasourceConnectionString);
// Parameters for report
//Map<String, Object> parameters = new HashMap<>();
HashMap parameters = new HashMap();
parameters.put("collectionId",Integer.parseInt(args[3]));
parameters.put("selectedItemId",Integer.parseInt(args[4]));
parameters.put("SUBREPORT_DIR",args[1]);
System.out.println("net.nmcollector.cpReports openReport parameters loaded");
// DataSource
Connection conn = null;
JasperPrint jasperPrint;
try {
String url = "jdbc:sqlite:" + datasourceConnectionString;
System.out.println("net.nmcollector.cpReports openReport url = " + url);
conn = DriverManager.getConnection(url);
System.out.println("net.nmcollector.cpReports openReport conn loaded");
} catch (Exception e) {
System.out.println("SQL connection error:" + e.getMessage());
}
try {
System.out.println("net.nmcollector.cpReports openReport ********* B4J Release Fails Here ********** call JasperPrint");
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
System.out.println("net.nmcollector.cpReports openReport call JasperViewer");
JasperViewer.viewReport( jasperPrint, false );
returnCode=0;
} catch (Exception e) {
System.out.println("Jasper Reports error:" + e.getMessage());
}
return returnCode;
}
}
Next, my B4X call:
B4X:
Private Sub Button_Open_Report_Click
Dim returnCode As Int
Try
Dim reportName As String = reportFileNamesList.Get(selectedReportIndex)
Dim subReportDir As String = File.DirData("nmcollector/Reports") & MainPage.pathSeparator
Dim sqlConnectionString As String = File.Combine(File.DirData("nmcollector"),"nmcswDB.sqlite")
'setup link to Report Wrapper
Dim jo As JavaObject
jo.InitializeStatic("net.nmcollector.reportwrapper.ReportWrapper")
'jo.InitializeNewInstance("net.nmcollector.reportwrapper.ReportWrapper", Null)
returnCode = jo.RunMethod("openReport", Array(Array As String(sqlConnectionString, subReportDir, reportName, selectedCollectionId, selectedItemId)))
Log("call to reports returnCode = " & returnCode)
Catch
Log("Open Report, error = [" & LastException & "]")
'Wait For (xui.MsgboxAsync(LastException, "Error")) Msgbox_Result (Result As Int)
Dim sf As Object = xui.Msgbox2Async(LastException, "Error", "okay", "", "", Null)
Wait For (sf) Msgbox_Result (Result As Int)
End Try
End Sub
Now, the error I encounter in B4J Release mode (Debug mode works just fine!):
B4X:
net.nmcollector.cpReports openReport ********* B4J Release Fails Here ********** call JasperPrint
reportspage$ResumableSub_Button_Open_Report_Click.resume (java line: 302)
java.lang.ClassCastException: class java.lang.NoClassDefFoundError cannot be cast to class java.lang.Exception (java.lang.NoClassDefFoundError and java.lang.Exception are in module java.base of loader 'bootstrap')
at anywheresoftware.b4a.BA.setLastException(BA.java:377)
at net.nmcollector.nmCollectorCP.reportspage$ResumableSub_Button_Open_Report_Click.resume(reportspage.java:302)
I can post more of the stack trace if you need it.
Last edited: