B4J Question Running JAR file location and name ?

wl

Well-Known Member
Licensed User
Longtime User
Hi,

Is there a way in obtaining (in code) the full path and filename of the jar file that is running ?

I would like to be able to put a configuration file next to the JAR file (with a different file extension).

Thanks !
 

Harris

Expert
Licensed User
Longtime User
Dim path as string = File.GetFileParent(filename).

You should know your file name since you created it?
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
That's not what I meant.

Indeed I know the filename of the JAR file, but since the JAR file can be put in any location (and the JAR file might be renamed as well), I would like to obtain the full path + filename of the running JAR.

Thanks
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I have no idea how one might interrogate some running jar to extract it's particulars...
Interesting - if at all possible...
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Thanks, but as I'm running the application inside a Linux shell ...

This is what I found in Java fora ... can this be embedded in B4J ?

B4X:
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
With inline java this seems to work:

B4X:
Dim obj As JavaObject = Me
Dim foo As String = obj.RunMethod("getRunningJAR", Null)
Log (foo)

and this as embedded Java:
B4X:
#if java
static public String getRunningJAR()
{
    try {
        return main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
    } catch (Exception e){
      throw new RuntimeException(e);
    }
}
 #End If
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Note that when running in debug mode there is no JAR and the return value refers to the folder in which the main.class resides.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…