Error running . jar out of eclipse with javafx

Asked

Viewed 291 times

1

I’m trying to run a .jar outside the eclipse and is returning the following ERROR:

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. javafx.fxml.Fxmlloader.loadImpl(Unknown Source) 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 gold) 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();
    }
}

1 answer

0


Apparently he’s not finding the file "/br/Ind/risc/view/Rootlayout.fxml". I suggest to open the file . jar and check if fxml is there. Another thing there is a space at the beginning of the informed path is this?

Ideal is that the layout files(fxml) stay inside the folder Resources of the project. Check this also.

Take a look here: https://stackoverflow.com/questions/17228487/javafx-location-is-not-set-error-message. Seems like a similar problem.

Follow in the footsteps:

1) Move main class to standard package

2) Set another path to Eclipse, and another path while running the JAR file (paste this into 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" ));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.