Error executing .jar out of eclipse with javaFX

1

I'm trying to run a .jar out of eclipse and are returning the following ERR :

  

C: \ Risc> java -jar GraphicalReport.jar   Exception in Application start method   Exception in thread "main" 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 org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main (JarRsrcLoa   der.java:58)   Caused by: java.lang.RuntimeException: Exception in Application start method   at com.sun.javafx.application.LauncherImpl.launchApplication1 (Unknown So   urce)   at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 155 (   Unknown Source)   at java.lang.Thread.run (Unknown Source)   Caused by: java.lang.IllegalStateException: Location is not set.   at javafx.fxml.FXMLLoader.loadImpl (Unknown Source)   at javafx.fxml.FXMLLoader.load (Unknown Source)   at br.ind.risc.application.ReportMain.initRootLayout (ReportMain.java:59)   at br.ind.risc.application.ReportMain.start (ReportMain.java:46)   at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162   (Unknown Source)   at com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175 (Unknown   Source)   at com.sun.javafx.application.PlatformImpl.lambda $ null $ 173 (Unknown Sourc   and)   at java.security.AccessController.doPrivileged (Native Method)   at com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 174 (Unknown S   ource)   at com.sun.glass.ui.InvokeLaterDispatcher $ Future.run (Unknown Source)   at com.sun.glass.ui.win.WinApplication._runLoop (Native Method)   at com.sun.glass.ui.win.WinApplication.lambda $ null $ 148 (Unknown Source)

Init method

/**
 * inicializa o layout
 */
public void initRootLayout() {
    try {
        FXMLLoader loader = new FXMLLoader();

        loader.setLocation(ReportMain.class.getResource(" /br/ind/risc/view/RootLayout.fxml"));

        rootLayout = (AnchorPane) loader.load(); //o erro está aqui.

        Scene scene = new Scene(rootLayout);        
        primaryStage.setScene(scene);
        primaryStage.centerOnScreen();
        primaryStage.show();
    } catch (IOException e) {

        e.printStackTrace();
    }
}
    
asked by anonymous 22.04.2016 / 21:18

1 answer

0

It seems that it is not finding the file " /br/ind/risc/view/RootLayout.fxml ". I suggest opening the .jar file and checking if the fxml is there. Another thing there is a space at the beginning of the path informed is that?

Ideally, layout files (fxml) should fall within the resources folder of the project. Check this, too.

Take a look here: link . Sounds like a problem to me.

Follow these steps:

1) Move main class to the standard package

2) Put another path to Eclipse, and another while running the JAR file (paste this in Main.java)

public  static  final  String sourcePath = isProgramRunnedFromJar ()  ?  "src/"  :  "" ; 
public  static  boolean isProgramRunnedFromJar ()  { 
    File x = getCurrentJarFileLocation (); 
    if ( x . getAbsolutePath (). contains ( "target" + File . separator + "classes" )){ 
        return  false ; 
    }  else  { 
        return  true ; 
    } 
} 
public  static  File getCurrentJarFileLocation ()  { 
        try  { 
            return  new  File ( Main . class . getProtectionDomain (). getCodeSource (). getLocation (). toURI (). getPath ()); 
        }  catch ( URISyntaxException e ){ 
            e . printStackTrace (); 
            return  null ; 
        } 
}

And after that at the beginning of the method you have to load files like this:

FXMLLoader loader =  new  FXMLLoader ( getClass (). GetResource ( sourcePath + "MainScene.fxml" ));
    
23.04.2016 / 03:01