简体   繁体   English

[Class] .class.getResourceAsStream(...)在某些目录的JAR文件中使用时返回null?

[英][Class].class.getResourceAsStream(…) returns null when used in JAR files in certain directories?

I have a very strange problem. 我有一个很奇怪的问题。 It appears that loading resources from inside a JAR file sometimes works and sometimes doesn't. 似乎从JAR文件内部加载资源有时可以工作,有时不能。

The Jar file consists only of the class files, the manifest and the test.txt . Jar文件仅包含类文件,清单和test.txt

When I pack it with Eclipse, it usually works well (the contents of test.txt can be accessed) but in some directories, the Resourceproblem.class.getResourceAsStream("/test.txt") returns null - although it's the same Jar file! 当我将其与Eclipse打包在一起时,它通常可以很好地工作(可以访问test.txt的内容),但是在某些目录中, Resourceproblem.class.getResourceAsStream("/test.txt")返回null尽管它是相同的Jar文件!

For example when I double click the jar file \\\\remotecomputer\\folder\\subfolder\\test.jar the test.txt inside the jar file could not be found. 例如,当我双击jar文件\\\\remotecomputer\\folder\\subfolder\\test.jar ,找不到jar文件中的test.txt But it's also sometimes (?) the case when the path name is very long and contains spaces (at least on Windows XP SP3). 但是,有时路径名称很长并且包含空格(至少在Windows XP SP3上如此)时,也是这种情况。

Any ideas? 有任何想法吗?

Sample code: 样例代码:

import java.io.*;

import javax.swing.*;

public class Resourceproblem {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Resourceproblem();
            }
        });
    }

    private Resourceproblem() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        JTextArea text = new JTextArea();
        text.setEditable(false);
        frame.add(text);
        frame.setVisible(true);

        try {
            InputStream in = Resourceproblem.class.getResourceAsStream("/test.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line = br.readLine();
            br.close();
            in.close();
            text.setText(line);
        } catch(Exception ex) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            ex.printStackTrace(pw);
            pw.flush();
            String s = sw.toString();
            pw.close();
            ex.printStackTrace();
            text.setText(s);
        }
    }
}

test.txt ist just a text file with a line for checking if the loading was successful. test.txt只是一个带有一行的文本文件,用于检查加载是否成功。

I have an idea, but I have little confidence that I know what your problem is. 我有个主意,但我几乎不知道我知道你的问题是什么。

I used eclipse to create a jar file from your example source several times; 我多次使用eclipse从您的示例源创建一个jar文件。 I stored the jar file in c:\\local and in c:\\local\\one\\two three\\four, using Win7. 我使用Win7将jar文件存储在c:\\ local和c:\\ local \\ one \\二三\\四。

I am unfamiliar with the use of "/" at the front of the resource filename in the getResourceAsStream() call. 我不熟悉getResourceAsStream()调用中资源文件名开头的“ /”用法。 Your sample program worked in each case either with or without the "/", but I would recommend removing it and seeing if that solves your problem. 您的示例程序在每种情况下都可以使用“ /”,也可以不使用“ /”,但是我建议您删除它,看看是否可以解决您的问题。 The file, in that case, goes in the same directory as the class that is accessing it. 在这种情况下,文件与正在访问该文件的类位于同一目录中。

I would have thought that the "/" would cause the program to look for the file at the root directory of the file system, but that doesn't seem to happen... 我本以为“ /”会导致程序在文件系统的根目录中查找文件,但这似乎没有发生……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM