I have the following code:
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
public static void main(String[] args) {
SWTTest test=new SWTTest(new Display());
test.run();
}
private Shell shell;
private Display display;
public SWTTest(Display main_display) {
display=main_display;
shell=new Shell(display, SWT.DIALOG_TRIM);
shell.setText("Test window");
shell.setBackgroundImage(new Image(display,"./image/blue_screen.jpg"));
}
public void run() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
这段代码可以编译和运行,但是问题在于当我把它导出为jar文件时。
我确认了一遍步骤:
右键点击项目->导出...->Runnable Jar文件->选择正确的启动配置,然后“完成”。
这会创建一个jar文件,但是当我从终端运行它时,会遇到以下错误:
Exception in thread "main" org.eclipse.swt.SWTException: i/o error (java.io.FileNotFoundException: ./image/blue_screen.jpg (No such file or directory))
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.graphics.ImageLoader.load(Unknown Source)
at org.eclipse.swt.graphics.ImageDataLoader.load(Unknown Source)
at org.eclipse.swt.graphics.ImageData.<init>(Unknown Source)
at org.eclipse.swt.graphics.Image.<init>(Unknown Source)
at test.SWTTest.<init>(SWTTest.java:31)
at test.SWTTest.main(SWTTest.java:13)
Caused by: java.io.FileNotFoundException: ./image/blue_screen.jpg (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at org.eclipse.swt.internal.Compatibility.newFileInputStream(Unknown Source)
... 6 more
为什么它在Eclipse上可以运行,但不能作为jar文件运行?为什么突然找不到图片了?
(我也会提到我向项目添加了一个名为“image”的新源文件夹,它现在与“src”文件夹并列 - 我想说的是“image”文件夹是我的Eclipse项目中的一个源文件夹)。那我错过了什么吗?
./
。 - m0skit0