简体   繁体   English

Eclipse - 引用Java资源

[英]Eclipse - referencing Java resources

This is probably just the IDE specifics. 这可能只是IDE的细节。 But with Eclipse, I'm interested in organizing my classes and resources a little more-so. 但是对于Eclipse,我有兴趣更多地组织我的类和资源。

As I understand it, the defaulted "src" directory is the place to throw all my resources. 据我所知,默认的“src”目录是抛出我所有资源的地方。 And if my resources reside in there I can reference them like this: 如果我的资源驻留在那里,我可以像这样引用它们:

ImageIcon icon = new ImageIcon("src/icon-16x16.png");

If I create a directory just for icons (inside the src/ directory), then I simply change the path as such: 如果我只为图标创建一个目录(在src /目录中),那么我只需更改路径:

ImageIcon icon = new ImageIcon("src/icons/icon-16x16.png");

So, now I want to do a separate directory for my resources outside the src/ directory. 所以,现在我想为src /目录之外的资源做一个单独的目录。

Here's my old structure: 这是我的旧结构:

prj.HelloWorld  (project)
-> src/ .........
-> src/images/ ........
-> src/ (default package) ......classes.java

Here's what I've cleaned up: 这是我清理过的东西:

prj.HelloWorld  (project)
-> classes/ .........
-> images/ ........
-> media / ........

Now, I'm interested in using a dedicated Project ( prj.Resources ), just for my common resources I can share in all projects. 现在,我有兴趣使用专用项目( prj.Resources ),只是为了我可以在所有项目中共享的公共资源。

prj.Resources  (project)
-> global_classes/ .........
-> global_images/ ........
-> global_media / ........

prj.HelloWorld  (project)
-> classes/ .........
-> images/ ........
-> media / ........

And then modify prj.HelloWorld by adding prj.Resources to the Projects Tab inside the Java Build Path. 然后通过将prj.Resources添加到Java Build Path中的Projects选项卡来修改prj.HelloWorld

(prj.HelloWorld > Properties > Java Build Path > Projects Tab > Add...) (prj.HelloWorld>属性> Java构建路径>项目选项卡>添加...)

I've done that so far, and any classes inside the prj.Resources/global_classes/ are successfully being detected. 到目前为止我已经完成了,并且成功检测到prj.Resources / global_classes /中的任何类。

But images inside global_images are not. 但是global_images中的图像不是。

ImageIcon icon0 = new ImageIcon("global_images/icon.png");

I know we can use resources located in different projects, but I'm missing something obvious here. 我知道我们可以使用位于不同项目中的资源,但我在这里遗漏了一些明显的东西。

YOu can not access global_images folder as its not in its root folder for HelloWorld Project. 你不能访问global_images文件夹,因为它不在HelloWorld Project的根文件夹中。

But you can give relative path as showing that project resource folder. 但是您可以将相对路径显示为显示项目资源文件夹。

      ImageIcon icon0 = new ImageIcon("../Resources/global_images/icon.png");

Path Information 路径信息

You could use the maven build infrastructure. 您可以使用maven构建基础结构。 There is a good eclipse integration. 有一个很好的eclipse集成。

Maven is in heavy use. Maven正在大量使用。 Maven uses best praxises / conventions like directory trees. Maven使用最好的praxises /约定,如目录树。

It does the library dependency management and provides many build tools. 它执行库依赖关系管理并提供许多构建工具。

  • src/main/java - java sources src / main / java - java源码
  • src/main/resources - for instance image resources src / main / resources - 例如图像资源
  • src/main/webapp - Web application root directory src / main / webapp - Web应用程序根目录
  • src/test/java - junit tests src / test / java - junit tests
  • src/test/resources - test resources src / test / resources - 测试资源
  • target - build directory target - 构建目录

There are features like multimodule projects, parent projects, inheriting parent's dependency version management. 有一些功能,如多模块项目,父项目,继承父项的依赖项版本管理。

Learn about how to construct your ImageIcon from a URL and how to find a URL to it relative to the location of the compiled .class file. 了解如何从URL构建ImageIcon以及如何相对于已编译的.class文件的位置查找到它的URL Anything not a .java file is copied to the output folder automatically by Eclipse, so your image will also go there. Eclipse不会自动将任何非.java文件复制到输出文件夹,因此您的图像也将会存在。 Doing it this way allows your program to keep working whether you package it in a .jar file or not, and whether they're in different projects or not, as long as the Java Build Path states their relationships correctly (it's used to determine the runtime classpath, which is searched by Class#getResource()). 这样做可以让程序继续工作,无论你是否将它打包成.jar文件,以及它们是否在不同的项目中,只要Java Build Path正确地表明它们的关系(它用于确定运行时类路径,由Class#getResource()搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM