[英]caused by java.lang.runtimeexception exception in application start method
I use javaFX without a fxml file needed all time. 我一直使用javaFX而不需要fxml文件。 When I run my app in IDE it works perfectly.
当我在IDE中运行我的应用程序时,它可以完美运行。 Only when I make jar file it does not work.
仅当我制作jar文件时,它不起作用。 Any help?
有什么帮助吗?
error : no such file in res\img\menu_bg.png
this file is in same folder like whole code 这个文件和整个代码一样在同一个文件夹中
code 码
public class InputStreamClass {
private ImageView imgViewBG;
public InputStreamClass() throws Exception {
InputStream isBG = Files.newInputStream(Paths.get("res/img/menu_bg.png")); // res/img/menu_bg.png
Image imgBG = new Image(isBG);
isBG.close(); // kedze je to InputStream a pracujeme zo suborom tak vzdy ho treba zatvarat
this.imgViewBG = new ImageView(imgBG); // pridame img do ImageView
this.imgViewBG.setFitWidth(1600); // sirka obrasku
this.imgViewBG.setFitHeight(900); // vyska obrasku
}
public ImageView getImageViewBG() {
return this.imgViewBG;
}
}
I assume you do not need to write to the image file. 我假设您不需要写入图像文件。
In this case the image should better be included in the jar itself. 在这种情况下,最好将图像包含在罐子本身中。 This prevents issues with paths.
这样可以防止路径问题。 Java tries to resolve relative paths relative to the working directory, which often is not the code folder.
Java尝试解析相对于工作目录(通常不是代码文件夹)的相对路径。
By adding the image to the jar file itself you can get the resource using a Class
instance, eg 通过将图像添加到jar文件本身,您可以使用
Class
实例获取资源,例如
// InputStream isBG = getClass().getResourceAsStream("/res/img/menu_bg.png");
// Image imgBG = new Image(isBG);
Image imgBG = new Image(getClass().getResource("/res/img/menu_bg.png").toExternalForm());
which works independent of the working directory. 它的工作独立于工作目录。
This assumes the file is stored as /res/img/menu_bg.png
entry in the jar file. 假定该文件在jar文件中存储为
/res/img/menu_bg.png
条目。
try this: In the same folder as your jar, put another folder name res. 尝试以下操作:在jar所在的文件夹中,放置另一个文件夹名称res。 Inside the res folder create a folder name img.
在res文件夹中,创建一个文件夹名称img。 Inside the img folder put your actually image with the name above.
在img文件夹中放入上面名称的实际图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.