无法打开以感叹号 (!) 结尾的目录中的资源 [英] Unable to open resources in directories which end with an exclamation mark (!)

查看:55
本文介绍了无法打开以感叹号 (!) 结尾的目录中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原因是 jar URL 中的!/"被解释为 JAR 文件名和 JAR 本身内路径之间的分隔符.如果目录名称以 ! 结尾,则目录末尾的!/"字符序列会被错误解释.在您的情况下,您实际上是在尝试使用以下 URL 访问资源:

jar:file:///d:/test1231@#!!!/test.jar!/images/Logo.png

该错误已存在近 12 年,并且不太可能修复.实际上我不知道如何在不破坏其他东西的情况下修复它.问题是使用的设计决定!作为 JAR 文件的 URL 方案中具有特殊含义的字符(分隔符):

jar:!/

由于感叹号是 URL 中允许的字符,它可能出现在 JAR 文件本身的 URL 中,也可能出现在 JAR 文件中的路径中,因此在某些情况下无法找到实际的!/" 分隔符.

The getResourceAsStream-method returns null whenever running the executable jar in a directory which ends with a exclamation mark.

For the following example, I have a Eclipse project the following directory structure:

src\ (Source Folder)
    main\ (Package)
        Main.java
    res\ (Source Folder)
        images\
            Logo.png

I'm reading the Logo.png as follows:

public static void main(String[] args) throws IOException {
    try (InputStream is = Main.class.getClassLoader().getResourceAsStream("images/Logo.png")) {
        Image image = ImageIO.read(is);
        System.out.println(image);
    }   
}

See the attachment for 2 test cases. First, the executable jar is started from the directory "D:\test123!@#" without any problems. Secondly, the executable jar is started from the directory "D:\test123!@#!!!", with problems.

Are directories ending with an exclamation mark not supported? Is the code wrong?

Thanks in advance.

解决方案

Probably because of this bug or any of the many similar bugs in the Java bug database:

http://bugs.sun.com/view_bug.do?bug_id=4523159

The reason is that "!/" in a jar URL is interpreted as the separator between the JAR file name and the path within the JAR itself. If a directory name ends with !, the "!/" character sequence at the end of the directory is incorrectly interpreted. In your case, you are actually trying to access a resource with the following URL:

jar:file:///d:/test1231@#!!!/test.jar!/images/Logo.png

The bug has been open for almost 12 years and is not likely to be fixed. Actually I don't know how it can be fixed without breaking other things. The problem is the design decision to use ! as a character with a special meaning (separator) in the URL scheme for JAR files:

jar:<URL for JAR file>!/<path within the JAR file>

Since the exclamation mark is an allowed character in URLs, it may occur both in the URL to the JAR file itself, as well as in the path within the JAR file, making it impossible in some cases to find the actual "!/" separator.

这篇关于无法打开以感叹号 (!) 结尾的目录中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆