B4J Question Get path to .JAR file running ?

gameone

Member
Licensed User
Hi, well i need to get path to .JAR file running.
But for now, look B4J don't give it.

I can only atm got path though "File.DirApp", but don't contains name of .JAR file running.
Any way to got it ?


If it's cross-plateform will be nice but otherwise, it's okay if is only for Windows.

Thanks u
 

sonicmayne

Member
Licensed User
Longtime User
After some reading of Stack Overflow and lots of trial and error this works for me (in release mode only), I've only been able to test it on Windows, it may work on other platforms though:

B4X:
#If Java
    public static String getJarFileName2() throws Exception {
        String className = main.class.getSimpleName() + ".class";
        String path =  main.class.getResource(className).toString();
        return java.net.URLDecoder.decode(path.substring(path.indexOf("/"), path.lastIndexOf("!")), "UTF-8");
    }
#End If

Call it similar to this:

B4X:
Sub GetJarFileName As String
    Dim JO As JavaObject = Me
    Return JO.RunMethod("getJarFileName2", Null)
End Sub

Notes: This assumes that the class you are calling the code from is the main class, if it isn't change the references to "main" in getJarFileName2 to the actual class you are using.
 
Upvote 0
Top