Get active execution location

/ Published in: Java
Save to your folder(s)

The returned file will either contain the folder from where your program is running or the executable jar file, if your program is an executable jar.

I'm using `Main.class` because this code is in my main method (which is in the class 'Main'). You can replace `Main.class` by `getClass()` if you want to use this code in a non-static method.

Copy this code and paste it in your HTML
  1. try {
  2. File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
  3. } catch (URISyntaxException e) {
  4. //do some error handling here
  5. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.