简体   繁体   English

class.getResource在Eclipse中与在jar中不一致的行为

[英]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: 我有两个资源文件:

  • one is in the class package, at the same level than the class file. 一个在类包中,与类文件位于同一级别。
  • one is outside the class package, at the same level than the package. 一个在类包之外,与包处于同一级别。

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在罐子的根目录
  • there are 3 additional files at the root (classpath, project, and manifest) 根目录下还有3个其他文件(类路径,项目和清单)
  • 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.

相关问题 Class.getResource()和ClassLoader.getResource()在可执行jar中的奇怪行为 - Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar Class.getResource在错误的jar中查找资源 - Class.getResource finds resource in wrong jar 使用class.getResource()在.jar中加载文件? - Use class.getResource() to load file within .jar? Class.getResource在Eclipse中返回错误的路径,并作为独立的exe - Class.getResource returns incorrect path in Eclipse and as a standalone exe Class.getResource() 返回 null - Class.getResource() returns null Java加载资源Class.class.getResource与 <classname> .class.getResource - Java loading resources Class.class.getResource vs <classname>.class.getResource class.getResource(“。”)返回null - class.getResource(“.”) returns null $ class.getResource从maven shade插件包装的胖jar失败了 - $Class.getResource failed from fat jar packed by maven shade plugin 在Java中,为什么class.getResource(“ / path / to / res”); 将文件复制到系统时,无法在可运行的jar中工作? - In Java, why does class.getResource(“/path/to/res”); not work in the runnable jar when copying files to the system? Java class.getResource()返回null - Java class.getResource() returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM