我应该如何在 Java 中使用 getResource() ?
这个问题在很多地方都有人提出,但也有很多细微的差别。 (例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 getResource 时,传入的字符串必须是绝对名称或相对于某个类有效。由于您使用的是
ClassLoader.getResource()
而不是Class.getResource()
,因此它必须是绝对路径。如果没有看到您的实际文件层次结构,我只能猜测“bin”是您编译的类和资源的根,“ibm1260”是该路径中的包/文件夹,并且“Chook.wav”存在于该文件夹中。如果是这种情况,那么您需要使用
/ibm1260/Chook.wav
(或者可能使用ibm1260/Chook.wav
,我通常不使用资源类加载器Lookups) 作为您传递给getResource()
的文件的名称。无论哪种方式,您都需要确保文件被复制到编译代码的位置,并且根文件夹位于类路径上。
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 usingClassLoader.getResource()
and notClass.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 potentiallyibm1260/Chook.wav
, I don't typically use the class loader for resource lookups) as the name of the file that you pass in togetResource()
.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.
getResource
和getResourceAsStream
方法用于访问类路径上的资源。您似乎正在尝试访问某些不在类路径上的资源。getResource
和getResourceAsStream
用于定位资源的逻辑本质上是相同的。这些方法之间的区别在于,一个返回 URL,另一个返回InputStream
。这一点也不难。您只需要了解类路径的工作原理...并确保您使用的资源名称可解析为您已放入类路径上的目录或 JAR 文件之一的正确位置的资源。
或者,如果资源不是您的应用程序的“一部分”,请不要以这种方式访问它。
The
getResource
andgetResourceAsStream
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
andgetResourceAsStream
use to locate resources is essentially the same. The difference between the methods is that one returns a URL, and the other anInputStream
.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.
您可以使用类似的方法
来找出 Java 到底在哪里找到您的资源。
You can use something like
to find out where exactly Java is expecting to find your resources.