简体   繁体   English

如何设置Eclipse项目以加载图像,使其也可以从命令行运行

[英]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. 我已经构建了一个Java应用程序,可以在运行时加载图像。 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. 我希望能够从Eclipse和命令行中运行该程序,并使其正确加载图像。 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. 设置项目是为了为输出创建一个bin目录,并将图像放在资源子文件夹中。 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. 但是,当我从Eclipse中运行程序时,当前的工作目录有所不同。

What am I missing? 我想念什么?

TIA 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. 按照注释中的建议,我尝试使用getResourceAsStream但是我不知道该如何传递给File构造函数。

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. 正在找到资源,因为temp不为null。

I think there's 2 solutions. 我认为有2个解决方案。
1) you specify an absolute path 1)您指定一个绝对路径
2) your image is in the classpath so you could load it via : 2)您的图片在类路径中,因此您可以通过以下方式加载它:

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. 在这种情况下,建议您使用Class.getResourceAsStream() ,然后将图像放在该类所在的目录中(或附近)。

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; ImageIcon类是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; getResource()方法采用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位于x / y / z / icons中,则无论在何处,PNG文件都必须位于x / y / z / icons / images中在那台计算机上。

TreeIcon is a class of mine, and its internals will not help you, so I'm not posting them. TreeIcon是我的一类,其内部结构对您无济于事,因此,我不会在此发布它们。 All it's doing here is providing a location for the PNG file I'm loading into an ImageIcon instance. 它所做的只是为我要加载到ImageIcon实例中的PNG文件提供位置。

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). 除了在具有目录结构的磁盘上工作外,它还可以在jar文件中工作(这是分发Java程序或库的常用方法)。 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. jar文件只是一个zip文件,jar / zip文件中的每个文件都有与之关联的目录,因此映像可以位于Java目录中正确目录中的jar中,就像java类在其目录中一样。

getResourceAsStream() returns a stream; getResourceAsStream()返回一个流; 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. 使用Java I / O必须弄清楚这是很平常的事情,不幸的是,没有在所有图像和情况下都可以使用菜谱的方法,所以我们不能仅仅告诉你它是什么。

EDIT 2: 编辑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. 我像这样设置了Eclipse项目。

目录

The input directory is added to the classpath (JavaBuildPath in Eclipse). 输入目录已添加到类路径(Eclipse中的JavaBuildPath)。

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;
    }
}

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

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