B4J Question GetSystemProperty & GetEnvironmentVariable

LucaMs

Expert
Licensed User
Longtime User
I can't remember (nor find) which library allows you to get the B4J application name at runtime (I would need it to set xui.SetDataFolder dynamically).

Is there a way to get all possible keys of GetSystemProperty and GetEnvironmentVariable, i.e. get two Maps?

I found Java code for the environment, so I could with Inline Java, but I would have to study how to get the Map in B4J and I don't think it's worth spending a whole day on a single instruction (SetDataFolder).
 

LucaMs

Expert
Licensed User
Longtime User
With:
B4X:
GetSystemProperty("sun.java.command", "")
I get the package name (I think); it is already useful, even if it is not the name of the application.
P.S. No, it's not the package name, it gives me this but also "Main".

Still searching :( ...
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The following function is certainly not ideal but it works a little, for jar files or in debug mode.
I'll use that one for now, but my question still stands.
B4X:
Private Sub GetAppName As String
    Dim appName As String

    appName = GetSystemProperty("sun.java.command", "")
    If appName.EndsWith("jar") Then
        appName = appName.SubString2(0, appName.Length - 4)
        Dim FileSeparatorChar As String = GetSystemProperty("file.separator", ".")
        appName = appName.SubString(appName.LastIndexOf(FileSeparatorChar) + 1)
    Else
        If appName.EndsWith(".main") Then
            appName = appName.SubString2(0, appName.Length - 5) ' Removes ".main"
            appName = appName.SubString(appName.LastIndexOf(".") + 1)
        End If
    End If
    
    Return appName
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Did you want to tell me how to receive the value of a java function in B4J, @DonManfred?
Otherwise it's not what I need (the name of the app, not of the "system" (PC)).
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
To get the package name, you can use JavaObject:
B4X:
Dim joBA As JavaObject
joBA.InitializeStatic("anywheresoftware.b4a.BA")
Log("Packagename: " & joBA.GetField("packageName"))

or if you try to use Me,
B4X:
Log(Me)
you will get something like this:
B4X:
class b4j.example.main

So, remove the "class " and ".main", you can get the package name.
B4X:
Sub PackageName As String
    Return Me.As(String).SubString2("class ".Length, Me.As(String).Length - ".main".Length)
End Sub

Log(PackageName)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
To get the package name, you can use JavaObject:
B4X:
Dim joBA As JavaObject
joBA.InitializeStatic("anywheresoftware.b4a.BA")
Log("Packagename: " & joBA.GetField("packageName"))

or if you try to use Me,
B4X:
Log(Me)
you will get something like this:
B4X:
class b4j.example.main

So, remove the "class " and ".main", you can get the package name.
B4X:
Sub PackageName As String
    Return Me.As(String).SubString2("class ".Length, Me.As(String).Length - ".main".Length)
End Sub

Log(PackageName)
https://www.b4x.com/android/forum/t...rty-getenvironmentvariable.157640/post-967631

With that code I get the name of the application, whether with the IDE open, debug or release, from the jar or from the exe.
It seems ok to me.

My question remains whether a library already exists, whether there is a direct way for the name, without having to eliminate the "main" or "jar" and, above all at this point, how the keys to the maps of Environment and System properties can be obtained .
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I found Java code for the environment, so I could with Inline Java, but I would have to study how to get the Map in B4J
I am not sure I get you correctly.

B4X:
Sub getSystemEnvironment As Map
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.System")
    Dim Env As Map = jo.RunMethod("getenv", Null)
    Return Env
End Sub

B4X:
Dim SystemEnvironment As Map = getSystemEnvironment
For Each Key As String In SystemEnvironment.Keys
    Log($"${Key} = ${SystemEnvironment.Get(Key)}"$)
Next
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
For SystemProperty,

B4X:
Sub GetSystemProp As Map
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.System")
    Dim Prop As Map = jo.RunMethod("getProperties", Null)
    Return Prop
End Sub

B4X:
Dim SystemProperty As Map = GetSystemProp
For Each Key As String In SystemProperty.Keys
    Log($"${Key} = ${SystemProperty.Get(Key)}"$)
Next
 
Upvote 0
Top