How to get the path of a running JAR file?

To get the path of a running JAR file, you can use the java.lang.Class class's getProtectionDomain method and then the getCodeSource method.

Here is an example of how to do this:

String path = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

This will return the path of the JAR file containing the MyClass class. You can use this technique in any class within your JAR file.

Keep in mind that this will only work if the JAR file is running as an executable JAR. If the JAR file is being used as a library and is not being executed directly, this technique will not work.

If you want to get the path of the JAR file that is executing the current code, you can use the following technique:

String path = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File jarFile = new File(path);
String jarDir = jarFile.getParentFile().getAbsolutePath();

This will give you the directory that the JAR file is located in, which should contain the JAR file itself.

I hope this helps! Let me know if you have any questions.