[英]class.getResource unconsistant behavior in-Eclipse vs in-jar
I'm testing how to express paths to resource files. 我正在测试如何表达资源文件的路径。 I get different results between Eclipse and command line with a jar.
我在Eclipse和带有jar的命令行之间得到了不同的结果。
Could you help me understand why there are differences? 您能帮助我理解为什么存在差异吗?
I've two resource files: 我有两个资源文件:
Eclipse. 日食。 This does work:
这确实有效:
package com.example.mypackage;
/* FileIO (project)
* |
* +---- src (source folder)
* |
* +---- com.example.mypackage (package)
* | |
* | +---- TestRes.java (class)
* | |
* | +---- users (resource file 1)
* |
* +---- test.txt (resource file 2)
*/
public class TestRes {
public static void main(String[] args) {
// Some resource names (relative, absolute)
String[][] res = {
{ "users" , "/com/example/mypackage/users" },
{ "../../../test.txt", "/test.txt" },
{ "." , "/com/example/mypackage/" }
};
// Get resource URL
String f = "Relative: %s\nAbsolute: %s\nURL: %s (%s)\n\n";
for (String[] r : res) {
String n0 = r[0];
String n1 = r[1];
try {
String url0 = TestRes.class.getResource(n0).getPath();
String url1 = TestRes.class.getResource(n1).getPath();
System.out.printf(f, n0, n1, url0, (url0.equals(url1)));
}
catch (Exception e) { e.printStackTrace(); }
}
}
}
Output: 输出:
Relative: users
Absolute: /com/example/mypackage/users
URL: /C:/Users/User/workspace/FileIO/bin/com/example/mypackage/users (true)
Relative: ../../../test.txt
Absolute: /test.txt
URL: /C:/Users/User/workspace/FileIO/bin/test.txt (true)
Relative: .
Absolute: /com/example/mypackage/
URL: /C:/Users/User/workspace/FileIO/bin/com/example/mypackage/ (true)
But when I export the project to a jar (right-click on FileIO, then export
), and run the jar with java.exe
at the command line, then class.getResource()
returns null
when trying to processing the second case ( "../../../test.txt"
) and third case ( "."
). 但是,当我将项目导出到jar中(右键单击FileIO,然后
export
),并在命令行中使用java.exe
运行jar时, class.getResource()
在尝试处理第二种情况时会返回null
( "../../../test.txt"
)和第三种情况( "."
)。
C:\Users\User\Desktop>java -jar TestRes.jar
Relative: users
Absolute: /com/example/mypackage/users
URL: file:/C:/Users/User/Desktop/TestRes.jar!/com/example/mypackage/users (true)
java.lang.NullPointerException
at com.example.mypackage.TestRes.main(TestRes.java:31)
java.lang.NullPointerException
at com.example.mypackage.TestRes.main(TestRes.java:31)
I checked the structure of the jar: 我检查了罐子的结构:
test.txt
is at the root of the jar test.txt
在罐子的根目录 TestRes.class
and users are in com\\example\\mypackage
folder TestRes.class
和用户位于com\\example\\mypackage
文件夹中 When running from Eclipse, the IDE is simulating the runtime class loader behavior by mapping to basic file system operations over src and bin folders. 从Eclipse运行时,IDE通过映射到src和bin文件夹上的基本文件系统操作来模拟运行时类加载器的行为。 The simulated behavior may not be an exact match for the real runtime behavior in all the corner cases.
在所有极端情况下,模拟的行为可能都不与实际的运行时行为完全匹配。 In this particular case, it looks like the simulator is not preventing the use of paths not supported at runtime.
在这种特殊情况下,模拟器似乎并未阻止运行时不支持的路径的使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.