We can use the getClass() method of Object class in Java that returns the currently running class, which further can be used with the getPath() method to get the path of the current working directory.
In Java, we can use the following code snippets to get the path of a running JAR file. // static String jarPath = ClassName. class . getProtectionDomain() .
A Java class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine.
The following code snippet will do this for you:
final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
replace MyClass with your main class
The resulting File object f represents the .jar file that was executed. You can use this object to get the parent to find the directory that the .jar is in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With