简体   繁体   中英

How to set up Eclipse project to load an image so that it also runs from the command line

I've built a Java application that loads an image at runtime. The location of the image is fixed relative to the project.

I would like to be able to run the program from both within Eclipse and the command line and for it to load the image correctly. However, I can only do one or the other but not both. This seems like such a trivial thing to want to do but I can't find out how to do it.

The project is set up so that it creates a bin directory for the output and puts the image in a resources sub-folder. This is fine when running from the command line as I can write my code to look in that sub folder for the file.

But when I run the program from within eclipse the current working directory is different.

What am I missing?

TIA

Update - adding some code

This is what I had originally:

BufferedImage awtImage = ImageIO.read(new File(System.getProperty("user.dir") +  "/resources/image-name.png"));

Following the advice in the comments I am trying to use getResourceAsStream but I don't know what to pass to the File constructor.

InputStream temp = MyClass.class.getResourceAsStream("resources/image-name.png");
BufferedImage awtImage = ImageIO.read(new File(???));

The resource is being found because temp is not null.

I think there's 2 solutions.
1) you specify an absolute path
2) your image is in the classpath so you could load it via :

YouClass.class.getResourceAsStream("YourImg.png");

The working directory, if that's really what you mean, is not a great place to load an image from. It appears that you have an image that you would distribute with your finished program so that the program could use it. In that case, I suggest that you use Class.getResourceAsStream() , and put the image in the directory with (or near) that class.

EDIT:

Here is code I used in one of my programs for a similar purpose:

ImageIcon expandedIcon = null;
// ...
    expandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Expanded.png"));

The ImageIcon class is part of Swing; I don't know if you're using that, but this should serve to show you the idea. The getResource() method takes a URL; again, you might need something a little different. But this shows the pathname relative to the path of the class on which the method is called, so if TreeIcon is in x/y/z/icons, the PNG file needs to be in x/y/z/icons/images, wherever that is on that computer.

TreeIcon is a class of mine, and its internals will not help you, so I'm not posting them. All it's doing here is providing a location for the PNG file I'm loading into an ImageIcon instance.

In addition to working on a disk with a directory structure, this also works in a jar file (which is a common way to distribute a java program or library). The jar file is just a zip file, and each file in the jar/zip file has its directory associated with it, so the image can be in the jar in the correct directory just as the java classes are in their directories.

getResourceAsStream() returns a stream; if you want to use that byte stream to load as an image, find a class that converts an stream to something your image class can use as a constructor or in a load method and hook them up. This is a common thing to have to figure out with Java i/o, unfortunately there is no cookbook way to do it across all images and situations, so we can't just tell you what it is.

EDIT 2:

As from the comment, try:

ImageIO.read(new File(MyClass.class.getResource("resources/image-name.png");

I set up my Eclipse projects like this.

目录

The input directory is added to the classpath (JavaBuildPath in Eclipse).

Java构建路径

Finally, you access the image and / or text files like this.

private BufferedImage getIconImage() {
    try {
        return ImageIO.read(getClass().getResourceAsStream(
                "/StockMarket.png"));
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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