查找Jar文件的自我位置

Tap*_*ose 0 java jar path

有没有什么方法可以通过编程方式找到jar分叉的当前位置?假设我正在执行Jar D:/Jars/myjar.jar,那么我可以从代码中获取该路径吗?


@AljoshaBre我试过这个

try {
        File file = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
        System.out.println(file.getAbsolutePath());
    } catch (Exception e1) {

        e1.printStackTrace();
    } 
Run Code Online (Sandbox Code Playgroud)

投掷NPE.:(

System.out.println(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
Run Code Online (Sandbox Code Playgroud)

仅显示client.jar.不是绝对的道路.它在一个罐子里面.

nul*_*ent 7

有一种方法:

return new File(ClassA.class.getProtectionDomain().getCodeSource().getLocation().toURI().g??etPath());
Run Code Online (Sandbox Code Playgroud)