B4J Question [SOLVED] Why would completion of JavaObject call close my B4J Application?

cjpryor

Active Member
Licensed User
A bit stumped here and could not find an answer in the forums. I hope it is something obvious!

I followed the example of the "Accessing third party Jar with #Additionaljar and JavaObject - Picasso" tutorial for B4A and adjusted it for B4J (see code below) and it works great except for one thing. When the jar completes its tasks my entire B4J application closes! No errors are thrown and I cannot figure out why even using the debugger. As far as I can tell the application closes without going through the B4XPage_CloseRequest on the main page.

Anyway, here is my B4X code. I am sure it could be written better but I am still learning/experimenting. :) Any suggestions as to why when this completes my application closes without error?

Call to my custom java code that displays Jasper Soft Report Viewer:
Private Sub Button_Open_Report_Click
   
    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.InitializeNewInstance("net.nmcollector.reportwrapper.ReportWrapper", Null)
        jo.RunMethodJO("main", Array(Array As String(sqlConnectionString, subReportDir, reportName, selectedCollectionId, selectedItemId)))

    Catch
       
        Log("Open Report, error = [" & LastException & "]")
        Dim sf As Object = xui.Msgbox2Async(LastException, "Error", "okay", "", "", Null)
        Wait For (sf) Msgbox_Result (Result As Int)
       
    End Try
   
    Log("why does program close when I close report viewer?")
   
End Sub

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
main method = entry point of a separate Java program. The code in this jar, probably kills the process at the end of the execution. Maybe to return the relevant exit code.
Either find the actual API that should be called or run it with jShell.

 
Upvote 0

cjpryor

Active Member
Licensed User
Thanks Erel! I wrote the java program so I can define what get's called. jShell looks intriguing ... this is the first time I have heard of it!

p.s, I learned to program in the late 70's and got my official education in computer science in the early 80's. Everything after (including Java) that has been hit and miss learning on my own.

:)
 
Last edited:
Upvote 0

cjpryor

Active Member
Licensed User
@Erel I guess I misunderstood your post. I thought you were saying my call to main in my external Java program was killing my B4J program (I do not understand why this would happen but I thought this is what you were saying). I changed my java program to call a method other than main but my B4J program still terminates when the external java program has done its job and closes. The external java program works as expected by opening the report in a report viewer. I do not expect my external java program to continue running after it has displayed the report so I do terminate it.

This is the new line in B4X. Note that I am no longer calling main and the method I call is not dependent on main in any way.

B4X:
returnCode = jo.RunMethodJO("openReport", Array(Array As String(sqlConnectionString, subReportDir, reportName, selectedCollectionId, selectedItemId)))

Are you saying I need to keep the external java program running in order not to terminate my B4J program when the external java program has done its job?

p.s. I will be happy to provide the external java program code, as ugly as it is, if that will help.

Thanks again.
 
Upvote 0

cjpryor

Active Member
Licensed User
I've been exploring the JavaObject and it seems I can use the following for my external jar but it does not seem to make a difference in terms of my B4J application closing when the external java application is done:

B4X:
        Dim jo As JavaObject
        jo.InitializeStatic("net.nmcollector.reportwrapper.ReportWrapper")
        returnCode = jo.RunMethod("openReport", Array(Array As String(sqlConnectionString, subReportDir, reportName, selectedCollectionId, selectedItemId)))

Thanks again to Erel and to anybody for any suggestions.
 
Upvote 0

cjpryor

Active Member
Licensed User
I isolated the source of my B4J application closing but I am not sure what to do about it. It is directly related to my call to the JasperReports Viewer (below). If I comment that line out in my external java application my B4J program does not close when my external java application closes. I will continue to investigate that connection. Perhaps others have experienced similar results in their java applications.

Here is an excerpt from my external Java program. The offending line is the call to JasperViewer in my external java application. This call displays the report. When I close the JasperViewer my B4J application also closes.

Java:
            jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);

            JasperViewer.viewReport(jasperPrint);

            returnCode=0;
 
Upvote 0

cjpryor

Active Member
Licensed User
Lookie what I found (https://community.jaspersoft.com/wiki/swing-application-exits-when-closing-jasperviewer-frame)

Swing application exits when closing the JasperViewer frame


This happens if you directly use the JasperViewer class in your Swing application.
The viewer application implemented in this class should be considered more like a demo application that shows how the JRViewer component can be used in Swing applications to display reports.
Your application unexpectedly terminates when you close the report viewer frame because the JasperViewer class makes a call to the System.exit(0).
To get around this, use the constructor that allows you to set the isExitOnClose to "false". But you are encouraged to create your own viewer that uses the more basic visual component implemented by the JRViewer class. Feel free to copy what code portion you might want to keep from the supplied JasperViewer class.
 
Upvote 0

cjpryor

Active Member
Licensed User
Solution!!!

In my external java program:

Java:
            jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
          
            JasperViewer .viewReport( jasperPrint, false );

            returnCode=0;
 
Last edited:
Upvote 0
Top