B4J Question [Non-UI] Application get app.jar directory

imbault

Well-Known Member
Licensed User
Longtime User
Hi,

On Windows :
For a Non-UI app, how to get the application "app.jar" directory, specially when you call it from another directory

Example calling CreateShareIO.jar from parent dir, the arguments "H" ouputs File.DirApp

Java -cp Objects\CreateShareIO.jar io.create.main "H"
I get D:\b4j\IAUDITOR

I would like to get :
D:\b4j\IAUDITOR\Objects

Thanks
 

Daestrum

Expert
Licensed User
Longtime User
Simple example should do what you want
B4X:
Sub AppStart (Args() As String)
 Log(asJO(Me).RunMethod("whereAmI",Array(Me)))
End Sub
Sub asJO(o As JavaObject)As JavaObject
 Return o
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
#if java
import java.io.*;
import java.net.*;
 
public static String whereAmI(Class c) throws URISyntaxException, IOException{
 return new File(c.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
}
#End If

When run in ide (debug) returns (in my case) C:\b4j Problems\whereis app\Objects\bin
in release returns C:\b4j Problems\whereis app\Objects
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Hi @Daestrum, your code works like a charm, thank you, @Erel, that path should be accessible by a File.DirSomething, as this directory is very usefull in [Non-UI] applications

Thanks a lot
 
Upvote 0
Top