【发布时间】:2017-05-06 07:49:07
【问题描述】:
我正在 Eclipse 中创建应用程序。当我在 Eclipse 中运行它时,它可以工作。
这是图像的路径:C:\Users\example\workspace\example\src\images
这是我的代码:
public String name;
public Rectangle example;
public Image exampleImage;
public Example(String name) {
this.name = name;
exampleImage = new Image("./images/example.png");
example = new Rectangle(exampleImage.getWidth(), exampleImage.getHeight());
example.setFill(new ImagePattern(exampleImage));
}
我从另一个文件中的此代码访问图像。
public Example example;
public void buildExample(){
example = new Example("example");
borderPane.getChildren().add(example.getImage());
}
所有的 .java 都在 src
所有图片都在图片中
在 Eclipse 中,这些目录有效:
“./images/example.png”
“文件:./src/images/example.png”
当它创建一个 .jar 文件时,路径是
Example.jar\images\
所有 .class 文件都在 Example.jar
所有图像都在图像中
当我运行 .jar 时,出现此错误:
线程“JavaFX 应用程序线程”java.lang.IllegalArgumentException 中的异常:图像必须为非空
为什么它在 Eclipse 中而不是在 .jar 中工作?
【问题讨论】:
-
您能否显示您尝试加载图像的实际代码,而不仅仅是路径?
-
请将
jar tvf Example.jar的输出添加到问题中。 -
输出是什么意思?
标签: java image file javafx path