B4J Question [SOLVED] Is it possible to get the B4J Log results when executing "java -jar ?"

Solution
Redirect logging output to a file and then read the file.

B4X:
' Redirects Output to a file
' Usage: RedirectOutput(File.DirApp, "logs.txt")
Sub RedirectOutputToFile (Dir As String, FileName As String)
    #if RELEASE
        Dim out As OutputStream = File.OpenOutput(Dir, FileName, True) 'Set to True to append the logs
        Dim ps As JavaObject
        ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
        Dim jo As JavaObject
        jo.InitializeStatic("java.lang.System")
        jo.RunMethod("setOut", Array(ps))
        jo.RunMethod("setErr", Array(ps))
    #end if
End Sub
' Redirects Output back to the (standard) screen
Sub RedirectOutputToScreen (Dir As String, FileName As String)
    #if RELEASE...

AHilton

Active Member
Licensed User
Longtime User
Redirect logging output to a file and then read the file.

B4X:
' Redirects Output to a file
' Usage: RedirectOutput(File.DirApp, "logs.txt")
Sub RedirectOutputToFile (Dir As String, FileName As String)
    #if RELEASE
        Dim out As OutputStream = File.OpenOutput(Dir, FileName, True) 'Set to True to append the logs
        Dim ps As JavaObject
        ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
        Dim jo As JavaObject
        jo.InitializeStatic("java.lang.System")
        jo.RunMethod("setOut", Array(ps))
        jo.RunMethod("setErr", Array(ps))
    #end if
End Sub
' Redirects Output back to the (standard) screen
Sub RedirectOutputToScreen (Dir As String, FileName As String)
    #if RELEASE
        Dim out As OutputStream = File.OpenOutput(Dir, FileName, False) 'Set to True to append the logs
        Dim ps As JavaObject
        ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
        Dim fd As JavaObject
        fd.InitializeStatic("java.io.FileDescriptor")
        Dim jout As JavaObject
        jout.InitializeNewInstance("java.io.FileOutputStream", Array(fd.GetField("out")))
        ps.InitializeNewInstance("java.io.PrintStream", Array(jout, True, "utf8"))

        Dim jo As JavaObject
        jo.InitializeStatic("java.lang.System")
        jo.RunMethod("setOut", Array(ps))
        jo.RunMethod("setErr", Array(ps))
    #end if
End Sub
 
Upvote 2
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…