Last active
March 10, 2019 12:21
-
-
Save ezhov-da/31b466ccf8db1e618e337b6953a1687f to your computer and use it in GitHub Desktop.
java абсолютный путь к исполняемому jar файлу
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[code:]java[:code]import java.io.File; | |
import java.net.URL; | |
import java.net.URLDecoder; | |
/** | |
* Данный класс позволяет получить абсолютный путь к папке, где лежит исполняемый файл | |
* <p> | |
* @author ezhov_da | |
*/ | |
public class AbsolutePath | |
{ | |
/** | |
* Получаем абсолютный путь к папке, в которой лежит jar файл передаваемого класса | |
* <p> | |
* @param clazzFromLoadJar - класс для получения пути к jar файлу | |
* <p> | |
* @return абсолютный путь | |
* <p> | |
* @throws Exception | |
*/ | |
public static synchronized String getAbsolutePath(Class clazzFromLoadJar) | |
throws Exception | |
{ | |
URL url = clazzFromLoadJar.getProtectionDomain().getCodeSource().getLocation(); | |
File f = new File(url.toURI()); | |
String decoder = URLDecoder.decode(f.getParentFile().getAbsolutePath(), "UTF-8"); | |
return decoder; | |
[/code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment