How can I know the location information of the running JAR file?

Asked 1 years ago, Updated 1 years ago, 82 views

My source code runs in the JAR file (for example, foo.jar). At this time, I want to know what folder the running foo.jar is in in the source code.

So if there is foo.jar in C:\FOOO\, I would like to know the location information of the JAR file regardless of the execution location.

path executable-jar java jar

2022-09-21 16:27

1 Answers

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());

However, this may cause strange results if you load something other than a file type.


2022-09-21 16:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.