简体   繁体   English

如何使用相对路径而不是绝对路径

[英]How to use relative path instead of absolute

So I need to set an icon for Intellij IDEA plugin, but when I'm trying to get this icon from my project with new File(relative path) or getClass().getResource(relative path) .因此,我需要为 Intellij IDEA 插件设置一个图标,但是当我尝试使用new File(relative path)getClass().getResource(relative path)从我的项目中获取此图标时。 It can't find the files, only works with the absolute path.它找不到文件,只能使用绝对路径。 I have tried with the following relative paths:我尝试过以下相对路径:

  1. images/icon.png
  2. resources/images/icon.png
  3. main/resources/images/icon.png
  4. src/main/resources/images/icon.png

Icons path: src/main/resources/images/icon.png图标路径: src/main/resources/images/icon.png

Source code path: src/main/java/com/timetrack/plugin/MyClass.java源码路径: src/main/java/com/timetrack/plugin/MyClass.java

code:代码:

File file = new File("src/main/resources/images/running.png");
BufferedImage img = ImageIO.read(file);

or with this或者用这个

BufferedImage img = ImageIO.read(getClass().getResource("images/running.png"));

EDIT编辑

Need to mention that I'am using Gradle to build the project.需要提及的是,我正在使用 Gradle 来构建项目。 So the output directory looks like this:所以输出目录是这样的:

Icon path : build/resources/main/images/icon.png图标路径build/resources/main/images/icon.png

Compiled classes : build/classes/java/main/com/timetrack/plugin/MyClass.class编译类build/classes/java/main/com/timetrack/plugin/MyClass.class

Your resource string needs to start with a slash, since it is not in the same package as your class.您的资源字符串需要以斜杠开头,因为它与您的类不在同一个包中。

Paraphrased from the documentation of getResource :转述自getResource 的文档

  • If the name begins with a / , then the absolute name of the resource is the portion of the name following the / .如果该名称与一个开始/ ,则资源的绝对名称是以下所述的名字的部分/
  • Otherwise, the absolute name is of the form modified_package_name/name , where the modified_package_name is the package name of this class with / substituted for .否则,绝对名称的格式为modified_pa​​ckage_name/name ,其中 modified_pa​​ckage_name 是此类的包名称,其中/替换为. . .

In other words, the argument passed to Class.getResource is assumed to be in the same package as the Class itself, unless the argument starts with a slash.换句话说,假定传递给 Class.getResource 的参数与 Class 本身在同一个包中,除非参数以斜杠开头。

This is because the proper way to include resources in an application or library is to place them in the same package directory as the class that uses them.这是因为在应用程序或库中包含资源的正确方法是将它们放在与使用它们的类相同的包目录中。 The reason for doing this is the same reason we use packages.这样做的原因与我们使用包的原因相同。 For that matter, it's the same reason applications don't store all their files in the user's home directory or in C:\\ : because there is a real risk that other programs will choose the same name and will interfere with your program.就此而言,这与应用程序不将其所有文件存储在用户的主目录或C:\\原因相同:因为存在其他程序选择相同名称并干扰您的程序的真正风险。

Class.getResource searches the classpath for the requested resource. Class.getResource 在类路径中搜索所请求的资源。 If you package your icon as images/running.png , and a library also decides to package its image as images/running.png , then Class.getResource will search the classpath and return whatever it finds first.如果您将图标打包为images/running.png ,并且库也决定将其图像打包为images/running.png ,则 Class.getResource 将搜索类路径并返回它首先找到的任何内容。 Depending on the order of the classpath entries, either you will get the wrong image, or that other library will.根据类路径条目的顺序,您会得到错误的图像,或者其他库会。 The two are essentially stepping on each other.两者本质上是互相踩踏。

On the other hand, if you place your image at src/main/resources/com/timetrack/plugin/running.png , it's unlikely any other code will be using that package, so your odds of a collision are mimimal.另一方面,如果您将图像放置在src/main/resources/com/timetrack/plugin/running.png ,则任何其他代码都不太可能使用该包,因此发生冲突的可能性很小。 And because this was intended to be the most common use case, using it this way is easier: You can retrieve the image URL with just MyClass.class.getResource("running.png") .并且因为这是最常见的用例,所以以这种方式使用它更容易:您可以仅使用MyClass.class.getResource("running.png")检索图像 URL。

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

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