尝试使用Launch4J生成.exe时为什么会出现此异常?

时间:2020-10-21 23:16:48

标签: java javafx arraylist launch4j

我有一个运行中的JAR,它使用了来自外部目录的某些资源,而使用了来自其自身的其他资源(使用“ getResourceAsStream”,它们都是图像)。该程序包含一个用JavaFX FXML创建的GUI(以及库“ Controls FX”),该GUI对SQLite数据库进行查询。当我尝试运行Launch4J成功创建的.exe文件时,它抛出ArrayIndexOutOfBoundsException,是指我尝试将ArrayList中的图像分配给按钮的那一行(按钮是FX中包含的普通按钮) )。

我尝试过的事情:

  • 在Launch4J的类路径中包括库。同样的例外。

  • 更改最小和最大JRE版本。同样的例外。

  • 使用相同的建筑属性和相似的图像存储(由使用“ getResourceAsStream”从JAR内部获取图像的静态枚举填充的ArrayList)从不同项目的JAR中创建一个.exe。 。它确实有效,所以问题一定在某个地方。

  • 确保在尝试访问数组列表之前,该数组列表已被图像填充(图像的索引为0)。我的意思是,JAR可以完美运行,但是我仍然必须仔细检查。当我在Netbeans或JAR中运行它时,它已被填充,但是奇怪的是,Launch4J在显示异常之前不会打印我的消息。

  • 尝试通过相对路径而不是从应该存储图像的ArrayList访问图像。异常发生变化,现在它指向另一个类,在该类中,我还尝试访问存储在ArrayList中的图像。如果我也通过相对路径获得该图像,则异常会转移到另一个位置。

  • 也使用“ getResourceAsStream”从外部目录获取图像。与以前相同:异常仅出现在代码的另一点。

  • 改为使用Jar2Exe。该exe也会生成,但不会运行。

总而言之:所有异常似乎都与图像的获取有关,当它暗示访问存储在ArrayList中的JAR内部的资源时,某种程度上不起作用,但仅适用于该特定项目(它在另一个项目中起作用) )和.exe文件(可在Netbeans和JAR中使用)。

谢谢。

Launch4J中的堆栈跟踪:

Executing: C:\Users\Setito\Desktop\DAM\Proyecto_fin_de_grado\PVZHeroes-Stats\PVZHeroes-Stats.exe
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
file:/C:/Users/Setito/Desktop/DAM/Proyecto_fin_de_grado/PVZHeroes-Stats/PVZHeroes-Stats.exe!/pvzheroes/clases/FXMLDocument.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at pvzheroes.clases.PVZHeroesStats.start(PVZHeroesStats.java:28)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    ... 1 more
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at pvzheroes.clases.inicializaciónComponentes.ConfigurarBotón(inicializaciónComponentes.java:98)
    at pvzheroes.clases.inicializaciónComponentes.<init>(inicializaciónComponentes.java:59)
    at pvzheroes.clases.FXMLDocumentController.initialize(FXMLDocumentController.java:584)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception running application pvzheroes.clases.PVZHeroesStats

代码的相关部分是:

  1. 包含jar文件中路径的枚举:
public static enum RUTAS_DENTRO_DE_JAR {
        TIPOS("métodosCompartidos/iconosTipos"),
        // More paths
        public String ruta;
        RUTAS_DENTRO_DE_JAR(String cadena) {
            ruta = cadena;
        }
  1. 称为用图像填充ArrayList的方法的部分。
private static ArrayList<Image> ICONOS_TIPOS = new MétodosCompartidos().AsignarImágenesJAR(constantes.RUTAS_DENTRO_DE_JAR.TIPOS.ruta, MétodosCompartidos.class);
           
  1. 获取图像并返回ArrayList的方法(它使用另一种方法:“ ObtenerNombresDirectorioJAR”,该方法从第一个参数表示的特定JAR目录返回所有文件名,从第二个参数定义的类开始)。 / li>
public static ArrayList<Image> AsignarImágenesJAR(String ruta, Class clase) throws IOException {
       ArrayList<String> ArrayNombres = ObtenerNombresDirectorioJAR(ruta, clase);
       ArrayList<Image> ArrayImágenes = new ArrayList<>();

       /**
        * Rellenamos ArrayImágenes con las imágenes
        */
       try {
           for (int i = 0; i < ArrayNombres.size(); i++) {
               Image imagen = new Image(MétodosCompartidos.class.getResourceAsStream(ArrayNombres.get(i)));
               ArrayImágenes.add(imagen);
           }
       } catch (NullPointerException e) {
           new DiálogoGenérico(constantes.TIPOS_DIÁLOGO.SIN_FONDOS.Cadena);
           System.exit(0);
       }
       return ArrayImágenes;
   }
  1. 引发异常的地方:另一个试图为按钮设置图像的方法(第二行)。 “ constantes.IMAGENES.TIPOSG.Comunes”是对枚举的引用,可以更轻松地访问以前创建的ArrayList。
c.ImagenesBoton = constantes.IMÁGENES.TIPOSG.Comunes;
c.ImagenBotón.setImage(c.ImagenesBoton.get(0));

0 个答案:

没有答案