Java:getResource图片,无法找到。 [英] Java: getResource image, cannot be found.

查看:317
本文介绍了Java:getResource图片,无法找到。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在Java中发现了一个错误(使用Eclipse)。我想将资源文件夹中的图像加载到应用程序中。使用以下行:

I just came up with an error in Java (using Eclipse). I want to load an image from the resource folder into the application. Using the follwoing lines:

URL url = this.getClass().getClassLoader().getResource("/resources/images/icon.png");
BufferedImage i = ImageIO.read(url);

但这导致 java.lang.IllegalArgumentException:input == null !例外。

我的文件夹结构是:

如何访问此图片?非常感谢!

How can I access this image? Thank you a lot!

推荐答案

getResource()返回 null 如果它无法在类路径中找到资源。

getResource() returns null if it can't find the resource on the classpath.

为了使用 getResource( )您需要资源在类路径上 。资源目录不在类路径中。在Eclipse中,您可以将 resources 文件夹添加到类路径中。或者在 srcServer 下创建一个新包 images 并将图标移出资源并进入 srcServer\images 以及您的源代码。

In order to use getResource() you need the resources to be on the classpath. The resources directory isn't on the classpath. In Eclipse, you could add the resources folder to the classpath. Or create a new package images under srcServer and move the icon out of resources and into srcServer\images along with your source code.

另一种方式是加载图像使用文件而不是将其作为类路径资源加载。

Another way would be to load the image using a File rather than loading it as a classpath resource.

这篇关于Java:getResource图片,无法找到。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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