java 如何获取 Jar 文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7740354/
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 to get Jar File Path
提问by Code Hungry
I have an Verification.jarfile which is checking some conditions in excel file. I want ot create a foldercalled Log at the same level where that Verification.jaris present.
我有一个Verification.jar文件,它正在检查 excel 文件中的一些条件。我想在Verification.jar所在的同一级别创建一个名为 Log的文件夹。
Thanks in Advence.........
提前谢谢......
EDIT
编辑
I am Having Folders as follows
我有如下文件夹
" Verification":- Build,dist,lib,nbproject,src,Test,build.xml
“验证”:- Build,dist,lib,nbproject,src,Test,build.xml
"Build"Contains Classes folder
“构建”包含类文件夹
"dist"contains Verification.jarand Lib folder
“dist”包含Verification.jar和 Lib 文件夹
"lib"Contains library files
“lib”包含库文件
I want to find the path of Verification.jar and create the Folder called Output.
我想找到 Verification.jar 的路径并创建名为Output的文件夹。
If i change the drive it should create the folder at the same level where Verification.jaris present.
如果我更改驱动器,它应该在存在Verification.jar的同一级别创建文件夹。
String path= getClass().getResource("").getPath();
Output:--getPath:/C:/Java/jdk1.6/bin/Verification/lib/
Output:--getPath:/C:/Java/jdk1.6/bin/Verification/lib/
回答by adatapost
Use URL Class.getResource(String name)
method.
使用 URL Class.getResource(String name)
方法。
package.ClassName.class.getClassLoader().getResource("package.ClassName");
EDIT:
编辑:
Have a look at great SO thread - How to get the path of a running jar file?suggested by @Hovercraft Full Of Eels
看看很棒的 SO 线程 - 如何获取正在运行的 jar 文件的路径?由@Hovercraft Full Of Eels 推荐
回答by Juan Lozano
The correct form is:
正确的形式是:
String directory = System.getProperty("user.dir");
If You are running a .jar file it returns the present working directory, this is the directory where the .jar is located...
如果您正在运行 .jar 文件,它将返回当前工作目录,这是 .jar 所在的目录...
Note that IDEs don't run the jar file, the run the .class file where the main method is located, so when running in an IDE this method returns the projects folder
请注意,IDE 不运行 jar 文件,而是运行 main 方法所在的 .class 文件,因此在 IDE 中运行时,此方法返回项目文件夹
PD: Sorry if I made grammar mistakes, English is not my native language.
PD:对不起,如果我犯了语法错误,英语不是我的母语。
回答by AdrianES
Is not the best solution, but I use this function. The jar file must be named same as the project. Works on windows and linux. If some can help me inprove it I will be honered.
不是最好的解决方案,但我使用了这个功能。jar 文件必须与项目名称相同。适用于 windows 和 linux。如果有人可以帮助我改进它,我会得到磨练。
public String getJarCurrentJar() {
String[] userDir = System.getProperty("user.dir").split("\"+System.getProperty("file.separator"));
String appName = "";
for (String temp : userDir) {
appName = temp; //Get the application name/jar
}
return System.getProperty("user.dir") + System.getProperty("file.separator") + "dist" + System.getProperty("file.separator") + appName+".jar";
}