我应该如何在 Java 中使用 getResource() ?

发布于 2024-11-16 03:42:46 字数 752 浏览 3 评论 0原文

这个问题在很多地方都有人提出,但也有很多细微的差别。 (例如 Java - getClassLoader().getResource() 让我发疯等。)我仍然无法做到工作。
这是一个代码片段:

        String clipName = "Chook.wav";
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        // URL url = classLoader.getResource(clipName);
        URL url = new URL("file:///Users/chap/Documents/workspace/the1620/bin/ibm1620/" + clipName);
        ais = AudioSystem.getAudioInputStream(url);

这有效 - 请注意,我已经硬编码了包含剪辑文件的目录的路径,该目录那里,并且与我的 .class 文件位于同一目录中。唉,注释掉的代码只是返回 url 的空值。

大多数其他帖子似乎都涉及 getResourceAsStream()。我想我应该使用 getResource() 。 有什么不同吗?

它只是不可能这么难。有什么线索吗?

This question is asked in numerous places, with myriad small variations. (Such as Java - getClassLoader().getResource() driving me bonkers among others.) I still can't make it work.
Here's a code snippet:

        String clipName = "Chook.wav";
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        // URL url = classLoader.getResource(clipName);
        URL url = new URL("file:///Users/chap/Documents/workspace/the1620/bin/ibm1620/" + clipName);
        ais = AudioSystem.getAudioInputStream(url);

This works -- note that I've hard-coded the path to the directory containing the clip file, which is there, and is in the same directory as my .class file. Alas, the commented-out code just returns a null value for url.

Most other posts seem to deal with getResourceAsStream(). I think I'm supposed to be using getResource(). Is that making the difference?

It just can't be this hard. Any clues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清旖 2024-11-23 03:42:46
String clipName = "Chook.wav";

使用 getResource 时,传入的字符串必须是绝对名称或相对于某个类有效。由于您使用的是 ClassLoader.getResource() 而不是 Class.getResource(),因此它必须是绝对路径。

如果没有看到您的实际文件层次结构,我只能猜测“bin”是您编译的类和资源的根,“ibm1260”是该路径中的包/文件夹,并且“Chook.wav”存在于该文件夹中。如果是这种情况,那么您需要使用 /ibm1260/Chook.wav (或者可能使用 ibm1260/Chook.wav,我通常不使用资源类加载器Lookups) 作为您传递给 getResource() 的文件的名称。

无论哪种方式,您都需要确保文件被复制到编译代码的位置,并且根文件夹位于类路径上。

String clipName = "Chook.wav";

When using getResource, the string you pass in must be either an absolute name or be valid relative to a certain class. Since you're using ClassLoader.getResource() and not Class.getResource(), it must be an absolute path.

Without seeing your actual file hierarchy, I can only guess that "bin" is the root of your compiled classes and resources, and "ibm1260" is a package/folder within that path, and "Chook.wav" exists in that folder. If that's the case, then you need to use /ibm1260/Chook.wav (or potentially ibm1260/Chook.wav, I don't typically use the class loader for resource lookups) as the name of the file that you pass in to getResource().

Either way, you need to make sure that the file is copied into the location of your compiled code and that the root folder is on the classpath.

╰ゝ天使的微笑 2024-11-23 03:42:46

getResourcegetResourceAsStream 方法用于访问类路径上的资源。您似乎正在尝试访问某些不在类路径上的资源。

getResourcegetResourceAsStream 用于定位资源的逻辑本质上是相同的。这些方法之间的区别在于,一个返回 URL,另一个返回 InputStream


事情不可能这么难。有什么线索吗?

这一点也不难。您只需要了解类路径的工作原理...并确保您使用的资源名称可解析为您已放入类路径上的目录或 JAR 文件之一的正确位置的资源。

或者,如果资源不是您的应用程序的“一部分”,请不要以这种方式访问​​它。

The getResource and getResourceAsStream methods are for accessing resources on the classpath. You seem to be trying to access some resource that is not on the classpath.

The logic that getResource and getResourceAsStream use to locate resources is essentially the same. The difference between the methods is that one returns a URL, and the other an InputStream.


It just can't be this hard. Any clues?

This is not that hard at all. You just need to understand how classpaths work ... and make sure that you use a resource name that resolves to a resource that you've put in the correct location in one of the directories or JAR files on the classpath.

Or if the resource is not "part of" your application, don't access it this way.

望笑 2024-11-23 03:42:46

您可以使用类似的方法

var x = MyClass.class.getResource(".");

来找出 Java 到底在哪里找到您的资源。

You can use something like

var x = MyClass.class.getResource(".");

to find out where exactly Java is expecting to find your resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文