class.getResource()和class.getCla
获取路径的区别如下:
(挺乱的!! 总结的也不一定对!!)
class.getResource()
-
根目录
/
=package 所在目录
, 一般就是 编译后的classes目录,也是class.getResource()
可以获取到的最上级的目录 。
对于jar包中的类来说,使用 "/xx" 获取到的目录是jar包实际所在路径下的地址,例如:
StringUtils.class.getResource("/org").getPath()
=file:/D:/mavenrepo/org/apache/commons/commons-lang3/3.0.1/commons-lang3-3.0.1.jar!/org
但是 使用只使用 "/" 获取到的路径 也是 编译后的classes目录 ! 即
/D:/,桌面/,项目/archcode/ludqtest/target/classes
所以? , 在jar包中要获取自己的资源文件的内容要使用
MyClass.class.getResource("/xx")
而不能只是MyClass.class.getResource("/")
- 当前目录
.
(或者空字符串也表示当前目录) 是类文件所在目录,也就是由 package 所表示的目录 。
对于jar包来说,获取的是jar包真实所在路径下 由 package 所表示的目录 。
App.class.getResource("/").getPath();
// 输出 /D:/xxx/target/classes/
App.class.getResource("").getPath();
// 输出 /D:/xxx/target/classes/com/xxx/test/
App.class.getResource("/myFile").getPath();
// 输出 /D:/xxx/target/classes/myFile , myFile必须是存在的文件或文件夹
App.class.getResource("/..").getPath();
// 输出 null, 不能获取比根目录还上级的目录
App.class.getResource("../../../../../../..").getPath();
// 输出 null,不能获取比根目录还上级的目录
new File(App.class.getResource("/").getPath()).getParent();
// 输出 /D:/xxx/target/ , 曲线救国
class.getClassLoader().getResource()
在 一般的java main项目中测试结果
-
当前目录
.
(或者空字符串也表示当前目录)表示classLoader
从 何处开始 加载类,一般也就是 编译后的 classes目录,也就是class.getResource("/")
对于jar包中的类来说,使用 "xx" 获取到的目录是jar包实际所在路径下的地址,例如:
StringUtils.class.getClassLoader().getResource("org").getPath()
=file:/D:/mavenrepo/org/apache/commons/commons-lang3/3.0.1/commons-lang3-3.0.1.jar!/org
但是 使用只使用 "" 获取到的路径 也是 编译后的classes目录 ! 即
/D:/,桌面/,项目/archcode/ludqtest/target/classes
所以? , 在jar包中要获取自己的资源文件的内容要使用
MyClass.class.getClassLoader().getResource("xx")
而不能只是MyClass.class.getClassLoader().getResource("")
- 而且该目录也是
classLoader
可以获取到的最上级的目录,所以也不能再向上获取更上级的目录了,如果尝试获取会返回null
在 Servlet 中 测试结果
所有类的classLoader获取的结果都一样!! 直接使用自己的类的classLoader就行
App.class.getClassLoader().getResource("/");
// 输出 null
App.class.getClassLoader().getResource("").getPath();
// 输出 /D:/xxx/target/classes/
// 等价于 App.class.getResource("/").getPath();
App.class.getClassLoader().getResource("myFile").getPath();
// 输出 /D:/xxx/target/classes/myFile , myFile必须是存在的文件或文件夹
// 等价于 App.class.getResource("/myFile").getPath();
总结:
在jar包中获取自己的jar包内的资源文件的内容要使用MyClass.class.getResource("/xx")
或
MyClass.class.getClassLoader().getResource("xx")
在jar包中获取所在项目的资源文件的内容要使用MyClass.class.getResource("/")
或
MyClass.class.getClassLoader().getResource("")
然后创建一个File对象,在根据getParent() 等文件函数获取到资源文件路径
打包之后的jar包获取运行时路径
当 jar包 以 java -jar some.jar
运行时,仅仅使用 class.getResource("/")
和 class.getClassLoader().getResource("")
时都是 null
。
而使用 class.getResource("/xxx")
时返回fileUri,
所以获取运行时路径的有两种方式
public static void main(String[] args) throws Throwable {
String runtimePath = getRuntimePath();
System.out.println("runtimePath1=" + runtimePath);
File file = new File(runtimePath, "myconfig.json");
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.write("somestr");
}
System.out.println(file.getAbsolutePath() + " content is "
+ MyFileUtils.readFile(file));
System.out.println();
System.out.println();
runtimePath = getRuntimePath2();
System.out.println("runtimePath2=" + runtimePath);
file = new File(runtimePath, "myconfig.json");
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.write("somestr");
}
System.out.println(file.getAbsolutePath() + " content is "
+ MyFileUtils.readFile(file));
}
/**
* 获取运行时路径
*/
public static String getRuntimePath() throws UnsupportedEncodingException {
URL url = App.class.getProtectionDomain().getCodeSource().getLocation();
// 转化为utf-8编码
String filePath = URLDecoder.decode(url.getPath(), "utf-8");
File file = new File(filePath);
if (filePath.endsWith(".jar")) {
// 可执行jar包运行的结果里包含".jar"
file = file.getParentFile();
}
return file.getAbsolutePath();
}
/**
* 获取运行时路径
*
* @throws URISyntaxException
*/
public static String getRuntimePath2()
throws UnsupportedEncodingException, URISyntaxException {
URL url = App.class
.getResource("/" + App.class.getName().split("[.]", 2)[0]);
// 转化为utf-8编码
String filePath = URLDecoder.decode(url.getPath(), "utf-8");
File file = null;
try {
URI fileUri = new URI(filePath);
file = new File(fileUri);
} catch (Exception e) {
file = new File(filePath);
}
return file.getParentFile().getParentFile().getAbsolutePath();
}