简体   繁体   English

由应用程序启动方法中的java.lang.runtimeexception异常引起

[英]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.

相关问题 Java 帮助:错误:java.lang.RuntimeException:应用程序启动方法异常 - Java Help: Error:java.lang.RuntimeException: Exception in Application start method AndroidRuntime:致命异常:主要由:java.lang.RuntimeException引起 - AndroidRuntime: FATAL EXCEPTION: main Caused by: java.lang.RuntimeException init 方法调用失败; 嵌套异常是 java.lang.RuntimeException: 无法启动 redis 服务器 - Invocation of init method failed; nested exception is java.lang.RuntimeException: Can't start redis server Android 引起的:java.lang.RuntimeException - Android Caused by: java.lang.RuntimeException 致命异常:main java.lang.RuntimeException:无法启动活动 - FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity 致命异常:main java.lang.RuntimeException:无法启动活动 - FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity 原因:java.lang.ExceptionInInitializerError原因:java.lang.RuntimeException - Caused by: java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException Android - Fatal Exception: java.lang.RuntimeException caused by java.lang.SecurityException on Percel.java - Android - Fatal Exception: java.lang.RuntimeException caused by java.lang.SecurityException on Percel.java 由 java.lang.RuntimeException 引起的线程“main”java.lang.ExceptionInInitializerError 中的异常:无法实例 KieServices - Exception in thread "main" java.lang.ExceptionInInitializerError caused by java.lang.RuntimeException: Unable to instance KieServices 应用程序启动方法中的异常由以下原因引起:java.lang.StackOverflowError - Exception in Application start method Caused by: java.lang.StackOverflowError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM