B4J Question How can I get the current working directory of a B4J program?

rgarnett1955

Active Member
Licensed User
Longtime User
Hi

I want to keep a database in a sub-directory of my app. I don't want to use windows data. I never trust anything Microsoft does.

How can I get the working directory of my app so I can set the db directory to appPath/Files?

Best regards
Rob
 

drgottjr

Expert
Licensed User
Longtime User
leaving aside the matter of trusting anyone more or less than microsoft for another day, in b4j you got your:
File.DirApp

and you got your:
Dim jo As JavaObject
jo.InitializeStatic("java.lang.System")
jo.RunMethod("getProperty", Array("user.dir")))

both give the same answer on my pc.
i use both file.dirapp and file.dirdata(), as the spirit moves me, to keep app-related files.

and, once again in relation to microsoft, i'm not sure what you mean by "microsoft data". i don't think it has anything to do with your current working directory, so try one of the above solutions and see how it works for you.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Take note, File.DirApp is returning current working directory, not folder that stores the .jar file.
 
Upvote 0

rgarnett1955

Active Member
Licensed User
Longtime User
leaving aside the matter of trusting anyone more or less than microsoft for another day, in b4j you got your:
File.DirApp

and you got your:
Dim jo As JavaObject
jo.InitializeStatic("java.lang.System")
jo.RunMethod("getProperty", Array("user.dir")))

both give the same answer on my pc.
i use both file.dirapp and file.dirdata(), as the spirit moves me, to keep app-related files.

and, once again in relation to microsoft, i'm not sure what you mean by "microsoft data". i don't think it has anything to do with your current working directory, so try one of the above solutions and see how it works for you.

I was referring to the C:\ProgramData directory. Also User data local and all that stuff. it's just to difficult for a humble engineer to understand. The problem with trusting Microsoft is they keep doing upgrades which seem to change stuff which works to stuff which doesn't. Unless you are on a big LAN where you might want to log on to different windows machines to get your LAN data, which I don't all of this directory stuff, active or otherwise is a moot point. All of this Microsoft stuff just creates a level of complexity that drains the blood from the face.

They have turned a Personal Computer which was supposed to be something simple into a monster.

In fact I don't trust any large corporations. As John Ralston Saul wrote in "Voltaire's Bastards" we all have been subject to a "coup d'etat in slow motion" so that now Big Corp runs the world as well as owning it. Another good writer on this was Sheldon Wolin. (He died in 2015.) He wrote of the takeover of the US Democracy by Big Corp as "Inverted Totalitarianism." I have read all this stuff and that's why I don't trust them. Perhaps you might take a look.

Best regards
Rob
 
Upvote 0

rgarnett1955

Active Member
Licensed User
Longtime User
leaving aside the matter of trusting anyone more or less than microsoft for another day, in b4j you got your:
File.DirApp

and you got your:
Dim jo As JavaObject
jo.InitializeStatic("java.lang.System")
jo.RunMethod("getProperty", Array("user.dir")))

both give the same answer on my pc.
i use both file.dirapp and file.dirdata(), as the spirit moves me, to keep app-related files.

and, once again in relation to microsoft, i'm not sure what you mean by "microsoft data". i don't think it has anything to do with your current working directory, so try one of the above solutions and see how it works for you.

Hi drgottjr,


I used File.DirApp an it gives me back the ...\Objects where the Jar File is so I wrote this to do what I wanted:

Gets the App directory:
'======================================================================================================
Public Sub makeDataDirUnderAppDir(dirName As String) As String
    'Get the App dir and check If this is the Objects directory - We don't want to put user files in here
    Dim cwd As String = File.DirApp

    Dim matcher1 As Matcher = Regex.Matcher("[^\\]+\\?$",cwd)
    
    Do While matcher1.Find
        If matcher1.Match.ToLowerCase.CompareTo("objects") = 0 Then
            cwd = Regex.Replace("[^\\]+\\?$", File.DirApp, "\Files")
            Exit
        End If
    Loop
    
    'Check if new cwd Exists and create if neccessary
    If File.IsDirectory (cwd,"") = False Then
        File.MakeDir(cwd, "Files")
    End If
    
    Return cwd
End Sub

Basically it gets the directory uses a Regex to find the last direcetory in the tree.

If the last directory is the Objects directory it removes this and then checks the path with the dirName input.

If it doesn't find it it creates it then returns the full path.

Best regards Rob
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
file.dirassets gets you the Files directory. it's not writeable, so i'm not sure why you'd want it.
 
Upvote 0
Top