B4J Question fx.ShowExternalDocument under Linux

peacemaker

Expert
Licensed User
Longtime User
HI, All

Who used inder Linux, what versions ?
Under Linux Mint 21 it does not work, does nothing.

1) How to open a web-site URL under Linux ?
2) @Erel, what this method does under Linux in jFX lib?
 
Solution
SOLVED this one question: browser opening starts to work, if to start the app ... without "sudo" !

This one code was checked OK that way (with command "xdg-open"):
B4X:
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.Runtime")
    jo.RunMethodJO("getRuntime", Null).RunMethod("exec", Array("xdg-open " & Address))

p.s. but troubles are not finished :) picture zooming again does not work (was OK at ... unknown Java & JavaFX files set...).
I'm full with desktop Linux, stopping.

peacemaker

Expert
Licensed User
Longtime User
Trying to use:

B4X:
    Sub Run_Command( cli As String, args As List)
    Log("Run_Command")

    sh.InitializeDoNotHandleQuotes( "cli", cli, args)
    sh.WorkingDirectory = File.Combine(File.DirData(Main.appname), Main.appname)
    sh.Run(-1)    '<<< changed

    Wait for cli_ProcessCompleted( Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
     '    #if Debug
    Log(Success & "; ExitCode = " & ExitCode)
    Log("StdOut = " &  StdOut)
    Log("StdErr = " &  StdErr)
    '    #end if
    If Success And ExitCode = 0 Then    'all is OK
    Else
        Log("StdErr = " & StdErr)
    End If

    End Sub
    
    Sub DetectOS As String
        Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
        If os.Contains("win") Then
            Return "windows"
        Else If os.Contains("mac") Then
            Return "mac"
        Else
            Return "linux"
        End If
    End Sub
    
    Sub OpenLink(url As String)
    #if B4J
    If DetectOS = "windows" Then
        Main.fx.ShowExternalDocument(url)
    Else
        Run_Command("xdg-open", Array As String(url))
    End If
    
    #else
        Dim p As PhoneIntents
        Dim inte As Intent = p.OpenBrowser(url)
        inte.WrapAsIntentChooser("")
        StartActivity (inte)
    #end if
End Sub

But under Linux i get:

Run_Command
false; ExitCode = -559038737
StdOut =
StdErr = org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "xdg-open" (in directory "/home/julia"): error=13, Access denied)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Not a linux user, but could it need sudo? as the error is acess denied
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Maybe can some Java code from this be used ?


Java:
import java.io.IOException;
import java.net.URISyntaxException;

public class URLHelper {
    public static void openUrl(String url) throws IOException, URISyntaxException {
        if (java.awt.Desktop.isDesktopSupported()) {
            java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

            if (desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
                java.net.URI uri = new java.net.URI(url);
                desktop.browse(uri);
            }
        }
    }
}

But how to use static subs ?

And FOUND THE SOLUTION of java.awt.Desktop.getDesktop: https://www.b4x.com/android/forum/threads/works-in-debug-but-not-in-release.139062/post-880393
It work under Windows (as ShowExternalDocument should !) and under Linux also.

UPD: but only once under Linux (under Win10 all clicks are OK), the first URL is opened in the web-browser, if to click secondly - the B4Xpage is hanging and system asks for ... wait for response or kill app. :(
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Linux - it's the f... sh... for UI app programmers, i'm sure now.
No one method works normally, sometimes partially, mostly with errors or limitations.

B4X:
Public Sub ShowURI(Address As String)
    Dim Desktop As JavaObject
    Desktop.InitializeStatic("java.awt.Desktop")
    Desktop = Desktop.RunMethod("getDesktop",Null)
  
    Dim supported As Boolean = Desktop.RunMethod("isSupported", Array("BROWSE"))
    If supported Then
        Dim URI As JavaObject
        URI.InitializeNewInstance("java.net.URI",Array(Address))
        Desktop.RunMethod("browse",Array(URI))
    End If
End Sub


Public Sub ShowURILinux(Address As String)
'    Dim shl As Shell
'    shl.InitializeDoNotHandleQuotes("shl", "xdg-open", Array As String (Address))
'    shl.Run(-1)
'---------------------------------------------------------------------
'    Run_Command("xdg-open", Array As String (Address))
'---------------------------------------------------------------------
'    ShowURI(Address)
'---------------------------------------------------------------------
    'Dim jo As JavaObject
    'jo.InitializeStatic("java.lang.Runtime")
    'jo.RunMethodJO("getRuntime", Null).RunMethod("exec", Array("xdg-open " & Address))
    'jo.RunMethodJO("getRuntime", Null).RunMethod("exec", Array("x-www-browser " & Address))
    '---------------------------------------------------------------------
'    Run_Command("x-www-browser", Array As String ("--no-sandbox", Address))
'---------------------------------------------------------------------
    Dim bash_script As String = $"
if which firefox > /dev/null; then
    firefox "%Address%" || { echo "firefox: error opening URL"; Exit 1; }
elif which xdg-open > /dev/null; then
    xdg-open "%Address%" || { echo "xdg-open: error opening URL"; Exit 1; }
elif which gnome-open > /dev/null; then
    x-www-browser --no-sandbox "%Address%" || { echo "x-www-browser: error opening URL"; Exit 1; }
elif which x-www-browser > /dev/null; then
    gnome-open "%Address%" || { echo "gnome-open: error opening URL"; Exit 1; }
Else
    echo "No web-browser for opening URL."
fi
"$
    bash_script = bash_script.Replace("%Address%", Address)
    File.WriteString(WorkingDir, "1.sh", bash_script)
    Run_Command("bash", Array As String ("1.sh"))
End Sub

And all Java system methods (sure, excluding calling Linux console utils) works fully OK under Windows.
I have no idea how other programmers open the web-browser under Linux distros thinking this it's sure way.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
SOLVED this one question: browser opening starts to work, if to start the app ... without "sudo" !

This one code was checked OK that way (with command "xdg-open"):
B4X:
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.Runtime")
    jo.RunMethodJO("getRuntime", Null).RunMethod("exec", Array("xdg-open " & Address))

p.s. but troubles are not finished :) picture zooming again does not work (was OK at ... unknown Java & JavaFX files set...).
I'm full with desktop Linux, stopping.
 
Last edited:
Upvote 0
Solution
Top