Java 如何获取当前正在执行的 jar 文件所在的目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2837263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the directory that the currently executing jar file is in?
提问by darrickc
I'm running a jar file in various locations and I'm trying to figure out how to get the location of the jar file that is running.
我在不同的位置运行一个 jar 文件,我试图弄清楚如何获取正在运行的 jar 文件的位置。
采纳答案by Robben_Ford_Fan_boy
Not a perfect solution but this will return class code base's location:
不是一个完美的解决方案,但这将返回类代码库的位置:
getClass().getProtectionDomain().getCodeSource().getLocation()
回答by Bozho
As noted in the comments, this is not a direct answer to the question, but may actually be what many people are looking for (the current directory of the executing code):
正如评论中所指出的,这不是问题的直接答案,但实际上可能是许多人正在寻找的(执行代码的当前目录):
new File(".").getAbsolutePath()
回答by Bill K
This seems like an iffy question to start out with.
这似乎是一个悬而未决的问题。
If I am running code in Java, it is running out of rt.jar and probably a dozen other jars.
如果我在 Java 中运行代码,它会用完 rt.jar 和其他十几个 jar。
The very first files to run will actually be in rt.jar, rt.jar it calls your main.
要运行的第一个文件实际上将在 rt.jar 中,它调用您的 main.jar。
If you write a class that allows you to tell what jar you are running and that class gets moved into a different jar, then you are not "running out of" the jar that contained your main any more.
如果您编写了一个类,允许您告诉您正在运行的 jar 并且该类被移动到另一个 jar 中,那么您就不会再“耗尽”包含您的 main 的 jar。
I think what you want is the command line that started your application, but if you CD'd to the directory first, then it might not have the full path to your jar.
我认为您想要的是启动您的应用程序的命令行,但是如果您先 CD 到该目录,那么它可能没有您的 jar 的完整路径。
Also, you may not be running from a jar--someone could have expanded your jar into classes in a directory and is running it that way.
此外,您可能不是从 jar 运行——有人可能已经将您的 jar 扩展为目录中的类并以这种方式运行它。
Personally I'd just wrap your program in a shell script launcher where you could look at your current location and the location of the jar file and then pass them into java.
就我个人而言,我只是将您的程序包装在一个 shell 脚本启动器中,您可以在其中查看当前位置和 jar 文件的位置,然后将它们传递到 java.lang.
回答by Joyal Augustine
Consider the following function
考虑以下函数
//This method will work on Windows and Linux as well.
public String getPath(){
URL rootPath = getClass().getResource("");
String URI=rootPath.toString().substring(9);
String[] currentPath=URI.split("myjar.jar!");
currentPath[0]=currentPath[0].replaceAll("%20"," ");
return currentPath[0];
}
回答by Vahan Margaryan
Here is my method to get execution path from anywhere
这是我从任何地方获取执行路径的方法
private static String GetExecutionPath(){
String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
absolutePath = absolutePath.replaceAll("%20"," "); // Surely need to do this here
return absolutePath;
}