为class.getResource()定义根类路径 [英] defining root classpath for class.getResource()

查看:122
本文介绍了为class.getResource()定义根类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse(Windows)中使用Java,并且正在尝试使用:

I'm using java in Eclipse (windows), and I'm trying to use:

myClass.getResource("/image.jpg"), 

在根位置"myProject/"中存在image.jpg.返回null.

While image.jpg exists in the root loacation "myProject/". It returns null.

我检查了许多其他线程,但没有一个解决了我的问题.

I checked many other threads but none of them resolved my issue.

所以我试图对此进行评估:

So I tried to evaluate this:

this.getClass().getResource("/").getPath();

它返回了"myProject/build/classes".

and it returned "myProject/build/classes".

所以我检查了该目录的项目配置,并在下面找到了完全相同的目录

So I checked the project's configuration for this dir, and found the exact same dir under

Java构建路径->源->默认输出文件夹

Java Build Path --> Source --> Default output folder

我的问题是:

  1. 为什么我的资源的默认目录是输出文件夹?

  1. why is the default dir of my resources is the output folder?

如前所述,我想从另一个文件夹获取资源,但是尝试调用"/../"的操作不起作用...然后如何加载未嵌套在类中的其他资源目录? (我可以更改输出目录,但我认为这非常丑陋……)

As mentioned, I want to get a resource from another folder, but attempts to call "/../" aren't working... How can I then load other resources which are not nested in my classes dir? (I can change my output dir, but I think it will be very ugly...)

推荐答案

您必须将它们放在"src/"文件夹的根目录中;-)

You must put them in the root of "src/" folder ;-)

SRC/文件夹已编译为BUILD/CLASSES/,因此,您在src/*中放置的所有内容都将归入classes/*

SRC/ folder is compiled to BUILD/CLASSES/, so all you put in src/* goes to classes/*

这篇关于为class.getResource()定义根类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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