0

The project works perfectly fine in the IDE. When I convert it to .jar it doesn't work anymore.

Because only the Main2.java class is converted to jar.

On my project, I have a controller class called Controller2.java

I want to convert the Main2.java + the controlleur2.java files together to a Jar file called final.jar for example! Because the application requires the controller to work.

How can I do that?

Edit:

When I run the jar file using cmd java -jar fileName.jar I get this error :

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)

in the FXML file I have :

<AnchorPane fx:controller="sample.Controller2">

and in Main2.java I have :

public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample2.fxml"));
    primaryStage.setTitle("Add new animal");
    primaryStage.setScene(new Scene(root, 426, 386));
    primaryStage.show();
}
  • What IDE are you using? How did you create the JAR file? Keep in mind that you don't package the `*.java` files when creating an executable JAR file but rather the `*.class` files (from compiling the project). – Slaw Jun 12 '20 at 23:50
  • 1
    I am using IntelliJ idea IDE. I created the jar file using the IDE: file-> project structure->add jar -> I choose the Main2 class then ok then I Build artifacts. There are class files created in the production folder: https://i.imgur.com/XtnpyaR.png –  Jun 12 '20 at 23:55
  • Do you get any error messages when you use the java -jar command in a command prompt window? – NormR Jun 13 '20 at 00:02
  • I used the Ide to convert to jar , and I double click on the jar file to run it ! I didnt use any command –  Jun 13 '20 at 00:11
  • Run it from the command line and see if it produces any errors – James_D Jun 13 '20 at 01:38
  • I Did and I get this error : ` Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) ` –  Jun 13 '20 at 02:49
  • 1
    That is most likely not the full stack trace (see [this Q&A](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors)). My guess is you're getting a "Location is required" error. That means the FXML file could not be located where you said it should be. If it works in the IDE but not the JAR file then, since you're using `getResource`, that typically means the FXML is not being included in the JAR file. See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – Slaw Jun 13 '20 at 04:43
  • This may also be helpful: https://stackoverflow.com/questions/29887524/how-to-add-resource-file-to-jar-in-intellij – Slaw Jun 13 '20 at 04:44

1 Answers1

0

Steps to covert to jar.

  1. Click on edit configuration on top right corner of intellij Add module dependencies

  2. Then click on modify options-> Add VM Options then add following module path.

    --module-path "" --add-modules javafx.controls,javafx.fxml

  3. Click on File -> Project structure. Then click on Artifacts. Add new artifact by clicking "+" -> Jar -> from modules and dependencies.

  4. Then click on Main class to select the main class.

  5. Then click on "+" icon and select file. Navigate to openjfx\bin and add all the dll files to the project. click on OK Plus icon to add

  6. Go to Build -> Build Artifacts -> Rebuid.

  7. Navaigate to build\artifacts<name> u will find the jar built

Shreyas D
  • 1
  • 2