java getResource()无效 [英] java getResource() not working

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

问题描述

这让我发疯了。
我在一个文件夹中有一个NetBeans项目,结构如下:

  MyProject 
----在$ src中构建
---- src
----资源

我的代码在包中。我想要做的是使用

  getClass()。getResource(/ resources / new.png);来自com.my.package包中的类的

但它只是拒绝工作! new.png图像位于资源文件夹中。
我错过了什么吗?



经过大量的游戏并在此处移动new.png图像以查看它何时会找到图像,它终于有效但只有当我放构建文件夹中的图像。
那么我需要做些什么呢?



编辑:抱歉,方法参数中的字符串错误。现在更正

解决方案

我不熟悉NetBeans,但问题可能是资源文件夹(或项目的根目录)不在类路径上,与 build 不同。



您需要配置NetBeans以将根文件夹或 resources 添加到类路径中,并相对于那里加载文件。



另请参阅此问题: Java - getClassLoader( ).getResource()让我疯狂


this is driving me crazy. I have a NetBeans project in a folder with the following structure:

MyProject
---- build
---- src
---- resources

in src my code is in packages. What I am trying to do is to use

getClass().getResource("/resources/new.png"); 

from a class in package com.my.package but it just refuses to work! The "new.png" image is in the resources folder. Am I missing something here?

After a lot of playing around and moving the "new.png" image here and there to see when it will find the image, it finally worked but only when I put the image in the build folder. So what do I have to do to make this work ?

edit: sorry, wrong String in method parameter. Corrected now

解决方案

I'm not familiar with NetBeans, but the problem is probably that the resources folder (or the root of the project, for that matter) is not on the classpath, unlike build.

You need to configure NetBeans to add the root folder, or resources to the classpath, and load the file relative to there.

See also this question: Java - getClassLoader().getResource() driving me bonkers

这篇关于java getResource()无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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