在jar之前提取文件路径

MrM*_*sUp 3 java jar filepath

假设您使用的是任何类型的fileIO,

public class example{

    public Example(){
    File file = new File(getClass().getProtectionDomain().getCodeSource().getLocation());

    //file IO process ....
    }
}
Run Code Online (Sandbox Code Playgroud)

通过使用getClass().getProtectionDomain().getCodeSource().getLocation()你将获得"example.jar"的完整路径.那么,如何获得该文件位置但没有"example.jar"?

(实际上是jar所在的文件夹,但不是jar本身的链接)

ars*_*jii 5

您可以使用

file.getParentFile()
Run Code Online (Sandbox Code Playgroud)

看到 File#getParentFile()