This question already has an answer here:
这个问题在这里已有答案:
- How to get the path of a running JAR file? 29 answers
如何获取正在运行的JAR文件的路径? 29个答案
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文件的位置。
5 个解决方案
#1
64
Not a perfect solution but this will return class code base’s location:
不是一个完美的解决方案,但这将返回类代码库的位置:
getClass().getProtectionDomain().getCodeSource().getLocation()
#2
11
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()
#3
7
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;
}
#4
2
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];
}
#5
0
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,rt.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。
#1
64
Not a perfect solution but this will return class code base’s location:
不是一个完美的解决方案,但这将返回类代码库的位置:
getClass().getProtectionDomain().getCodeSource().getLocation()
#2
11
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()
#3
7
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;
}
#4
2
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];
}
#5
0
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,rt.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。