如何在 Eclipse 中向我的 Java 项目添加资源文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27934796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a resources folder to my Java project in Eclipse
提问by redeagle47
I want to have a place to store my image files to use in my Java project (a really simple class that just loads an image onto a panel). I have looked everywhere and cannot find how to do this. How do I do this?
我想要一个地方来存储我的图像文件以在我的 Java 项目中使用(一个非常简单的类,它只是将图像加载到面板上)。我到处找,找不到如何做到这一点。我该怎么做呢?
I have tried adding a new folder to the project, adding a new class folder to the project, and adding a new source folder to the project. No matter what I do, I always get a IOException
. The folders always say they are on the build path, so I'm not sure what to do.
我尝试向项目添加一个新文件夹,向项目添加一个新的类文件夹,并向项目添加一个新的源文件夹。不管我做什么,我总是得到一个IOException
. 文件夹总是说它们在构建路径上,所以我不知道该怎么做。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PracticeFrame extends JFrame{
private static BufferedImage image;
Thread thread;
public PracticeFrame() {
super();
setPreferredSize(new Dimension(640,480));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main (String[] args) {
PracticeFrame pframe = new PracticeFrame();
try {
image = ImageIO.read(new File("/islands.png"));
} catch (IOException e) {
e.printStackTrace();
}
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,null);
}
};
panel.setBackground(Color.BLUE);
panel.repaint();
pframe.add(panel);
}
}
EDIT: Something that worked for me, and I have no idea why, was adding the main/res/
folder as a class folder and then removing it. I ran it while the /main/res/
was part of the build path as a class folder and it still didn't work. When i added it, i got a popup that told me something about excluded filters. But when i removed the folder from the libraries in the build path, and changed my file path to:
编辑:对我有用的东西,我不知道为什么,将main/res/
文件夹添加为类文件夹,然后将其删除。我在/main/res/
作为类文件夹的构建路径的一部分时运行了它,但它仍然无法正常工作。当我添加它时,我得到了一个弹出窗口,告诉我一些关于排除过滤器的信息。但是当我从构建路径中的库中删除该文件夹并将我的文件路径更改为:
image = ImageIO.read(new File("src/main/res/islands.png"));
I at least stopped getting the IOException
thrown. I must not be adding the image to the panel correctly, because it's not showing up, but at least it found the file (I think).
我至少停止了被IOException
抛出。我一定没有正确地将图像添加到面板,因为它没有显示出来,但至少它找到了文件(我认为)。
回答by Rami Del Toro
After adding a resource folder try this :
添加资源文件夹后试试这个:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("test.png");
try {
image = ImageIO.read(input);
} catch (IOException e) {
e.printStackTrace();
}
回答by Lautaro Cozzani
When at the "Add resource folder",
Build Path -> Configure Build Path -> Source (Tab) -> Add Folder -> Create new Folder
在“添加资源文件夹”时,构建路径 -> 配置构建路径 -> 源(选项卡) -> 添加文件夹 -> 创建新文件夹
add "my-resource.txt" file inside the new folder. Then in your code:
在新文件夹中添加“my-resource.txt”文件。然后在你的代码中:
InputStream res =
Main.class.getResourceAsStream("/my-resource.txt");
BufferedReader reader =
new BufferedReader(new InputStreamReader(res));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
回答by jones j alapat
Try To Give Full path for reading image.
尝试提供阅读图像的完整路径。
Example image = ImageIO.read(new File("D:/work1/Jan14Stackoverflow/src/Strawberry.jpg"));
示例图像 = ImageIO.read(new File("D:/work1/Jan14Stackoverflow/src/Strawberry.jpg"));
your code is not producing any exception after giving the full path. If you want to just read an image file in java code. Refer the following - http://docs.oracle.com/javase/tutorial/2d/images/examples/LoadImageApp.java
提供完整路径后,您的代码不会产生任何异常。如果您只想读取 java 代码中的图像文件。请参阅以下内容 - http://docs.oracle.com/javase/tutorial/2d/images/examples/LoadImageApp.java
If the object of your class is created at end your code works fine for me and displays the image
如果您的类的对象是在最后创建的,您的代码对我来说工作正常并显示图像
// PracticeFrame pframe = new PracticeFrame();//comment this
new PracticeFrame().add(panel);
回答by mannedear
To answer your question posted in the title of this topic...
要回答您在本主题标题中发布的问题...
Step 1--> Right Click on Java Project, Select the option "Properties"
步骤 1--> 右键单击 Java 项目,选择“属性”选项
Step 2--> Select "Java Build Path" from the left side menu, make sure you are on "Source" tab, click "Add Folder"
步骤 2--> 从左侧菜单中选择“Java Build Path”,确保您在“Source”选项卡上,单击“Add Folder”
Step 3--> Click the option "Create New Folder..." available at the bottom of the window
Step 4--> Enter the name of the new folder as "resources" and then click "Finish"
步骤 4--> 输入新文件夹的名称为“资源”,然后单击“完成”
Step 5--> Now you'll be able to see the newly created folder "resources" under your java project, Click "Ok", again Click "Ok"
Step 5--> 现在你可以在你的java项目下看到新创建的文件夹“resources”,点击“Ok”,再次点击“Ok”
Final Step --> Now you should be able to see the new folder "resources" under your java project
回答by dani bilel
Build Path -> Configure Build Path -> Libraries (Tab) -> Add Class Folder, then select your folder or create one.
Build Path -> Configure Build Path -> Libraries (Tab) -> Add Class Folder,然后选择你的文件夹或创建一个。
回答by vinsinraw
If aim is to create a resourcesfolder parallel to src/main/java, then do the following:
如果目标是创建一个与src/main/java并行的资源文件夹,请执行以下操作:
Right Click on your project > New > Source Folder
Provide Folder Name as src/main/resources
Finish
回答by Mandeep Singh Gill
Right click on project >> Click on properties >> Java Build Path >> Source >> Add Folder
右键单击项目>>单击属性>>Java构建路径>>源>>添加文件夹
回答by rupweb
If you have multiple sub-projects then you need to add the resources folder to each project's run configuration class path like so:
如果您有多个子项目,那么您需要将资源文件夹添加到每个项目的运行配置类路径中,如下所示:
Ensure the new path is top of the entries and then the runtime will check that path first for any resources (before checking sub-paths)
确保新路径位于条目的顶部,然后运行时将首先检查任何资源的路径(在检查子路径之前)