• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Eclipse Export JavaFX Project to Runnable Jar File - Nothing is Showing up?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to using Eclipse, JavaFx, and the Java language in general (came from VB.NET, Javascript, & PHP background).

So, I'm trying to do a simple test where I export a very simple JavaFX program in Eclipse, but I keep getting the following error message inside of the command line when I try to test to see why the exported .jar file never appears or never opens up when I doubleclick on the .jar file to run it:

"JavaFX runtime components are missing, and are required to run this application"



And my Java project structure looks like this:



I have tried to export the project as a "Runnable Jar File", but only to no avail. This must be a common issue, no?

I'd greatly appreciate any support.
 
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JavaFX libraries are not included in your JAR. You must add them to either the class path or the module path when you execute your JAR using the -cp switch or the --module-path switch.

It's much easier to build and run your application with Maven though.
 
Aaron Esteban
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan, and thank you for your quick response.

Are you referring to the "Java Build Path" in Eclipse? Is this the area where I add the JavaFX files? I thought that I've already included them in the classpath area:



How exactly would you handle this? Thanks again.


Stephan van Hulst wrote:The JavaFX libraries are not included in your JAR. You must add them to either the class path or the module path when you execute your JAR using the -cp switch or the --module-path switch.

It's much easier to build and run your application with Maven though.

 
Aaron Esteban
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, when I try to run the jar file in command line, I'm getting the following error messages:






Stephan van Hulst wrote:The JavaFX libraries are not included in your JAR. You must add them to either the class path or the module path when you execute your JAR using the -cp switch or the --module-path switch.

It's much easier to build and run your application with Maven though.

 
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted here: https://www.dreamincode.net/forums/topic/420784-eclipse-export-javafx-project-to-runnable-jar-file-nothing-is-showing/
 
Aaron Esteban
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, and I've successfully done this previously, but the problem that I'm having, or what doesn't quite make any sense to me, is that I'm trying to distribute a simple little JavaFX GUI application that I've made for family and friends, but I wouldn't want them to have to run any command line code to access the jar file. How would I distribute an executable file that won't encounter this type of problem? I apologize, but this is all new to me. Thanks again.
 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way to solve your problem is to build your application as a Jigsaw module using Maven and then releasing the JavaFX libraries with your module. Finally add a batch script that will execute your main application module.

Let's assume that you have a main class that looks like this:

To make your application a Jigsaw module, add a module-info.java file to the root package. That is the parent directory of the com directory in my example:

As you can see in my example, I named the module after the principal package that the module exports. You should do the same for your application.

Build your application with Maven. Here's an example POM:

This POM declares a dependency on javafx-fxml, which will automatically pull in most other JavaFX dependencies that you need.

It also configures the maven-jar-plugin that builds a JAR of your main application module so that the resulting JAR will contain references to all its dependencies. The main application module will be output in the target/dist/lib directory. The main class is also configured.

The POM configures the maven-dependency-plugin to copy all the dependencies of your application to the target/dist/lib directory. This means that when you build your application, both your application JAR and the JavaFX dependencies that it needs will be in the target/dist/lib directory.

After you build the application, you can run it from inside the target/dist folder using the following command:

You can make a batch script that contains this command and put it in the target/dist folder. You can now zip and distribute the dist folder and your users will only have to execute the batch script it contains.
 
Aaron Esteban
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I will definitely take a look over this and try to learn it. I am not familiar with Maven, but I see that a few folks have already recommend that I learn how to use it for my Java projects. It doesn't look like it's too hard to learn (not a long learning curve), but I guess I will find out for sure lol. Thanks again Stephan for your time and support. I really do appreciate you.
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan Thanks for the mini Maven tutorial.  I have picked it up to start learning Maven.  
Is there a main.fxml file available to satisfy the code in MyApplication?

Also could you describe how the java commandline you posted connects to the MyApplication program?
There are 9 jar files in the lib folder.  Are they all searched for a manifest file?
 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Is there a main.fxml file available to satisfy the code in MyApplication?


I didn't take the time to design anything, I just generated an empty UI with NetBeans. Here it is:

Also could you describe how the java commandline you posted connects to the MyApplication program?


The --module-path switch puts all the modules in the specified folder on the module path, which is similar to the class path, except modules use a stricter model for accessing code in other modules.

With the --module switch (or -m for short) you specify the name of the module that you want to act as the entry point of the application. Normally you would write -m com.example.app/com.example.app.MyApplication to specify both module name and main class name, but since the module is packaged as an executable JAR, Java gets the name of the main class from the JARs manifest, which I configured using the <mainClass> element in the Maven POM.

Note that there is a super confusing aspect about loading modules that this application doesn't really demonstrate, but that you might encounter in the future: Modules that are available on the module path aren't necessarily loaded at runtime. Java takes the module that you selected as the entry point, and uses its module descriptor (module-info.class) to recursively build a graph of modules that will be loaded. All classes that are loaded this way must be on the module path, but not all modules that are on the module path will be loaded. For instance, plugins are not directly referenced in the module descriptor of your application, and therefore will not be loaded automatically. If you want to use such a plugin in your application, you must explicitly tell Java to load them using the --add-modules switch.

Note that if you put a library on the class path instead of on the module path, it will be considered as part of the "unnamed module" and will not automatically load modules that are required in the module descriptor. That's why I recommended Aaron to make his application into a Jigsaw module and put it on the module path, because otherwise you will have to load all JavaFX modules explicitly using the --add-modules switch.

There are 9 jar files in the lib folder.  Are they all searched for a manifest file?


No. They are all searched for a module descriptor. The module descriptor determines the name of the module. You explicitly specify the name of the module that you want to use as the entry point. Java will only use the manifest of the module that you specified as the entry point to determine the main class.
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the fxml file and the detailed explanations.


Is this a class that is needed?

Do you expect the posted code to execute properly?
I get this:



D:\JavaDevelopment\Testing\MavenTests\CodeRanchJavaFX\target\dist>D:\Java\jdk-14\bin\java.exe --module-path lib -m com.example.app
Exception in Application start method
java.lang.reflect.InvocationTargetException
       at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.base/java.lang.reflect.Method.invoke(Method.java:564)
       at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
       at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
       at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.base/java.lang.reflect.Method.invoke(Method.java:564)
       at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
       at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
       at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
       at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NullPointerException: Location is required.
       at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
       at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
       at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
       at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
       at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
       at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
       at com.example.app@0.1.0/com.example.app.MyApplication.start(MyApplication.java:12)
       at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
       at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
       at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
       at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
       at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
       at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
       at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
       at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
       ... 1 more
Exception running application com.example.app.MyApplication

D:\JavaDevelopment\Testing\MavenTests\CodeRanchJavaFX\target\dist>MORE



I think I have copied what you posted.  
The example-app-0.1.0.jar file has the  main.fxml and module-info.class files at the root level.  
The MyApplication.class file is in the com.example.app folder.
The Manifest.MF file contains:


Manifest-Version: 1.0
Created-By: Maven Jar Plugin 3.2.0
Build-Jdk-Spec: 14
Class-Path: javafx-fxml-11.0.2.jar javafx-fxml-11.0.2-win.jar javafx-con
trols-11.0.2.jar javafx-controls-11.0.2-win.jar javafx-graphics-11.0.2.
jar javafx-graphics-11.0.2-win.jar javafx-base-11.0.2.jar javafx-base-1
1.0.2-win.jar
Main-Class: com.example.app.MyApplication

 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh the problem is caused by putting the main.fxml file in the root package. Resource paths that don't start with a forward slash are relative to the package of the class that you're using to load the resource. Putting it in the root package would have been correct if the resource path was "/main.fxml", but since it is "main.fxml" you have to put it in the com.example.app package. In general it's good practice to put resources in the same package as the classes that use them, but don't put them in the same folder. Use Maven's default directory layout to separate sources from resources.

If:
 MainController.java is inside src/main/java/com/example/app/

Then:
 main.fxml should be inside src/main/resources/com/example/app/

This way, the FXML file will end up in the same package in the final JAR file, but you can keep your source files physically separate from your resources in your project layout. Maven also treats sources and resources slightly different during the build phase.
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again,  Now the error is a missing class:
Do you have code for the MainController class?
 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:
Is this a class that is needed?


Yes. JavaFX is based on the MVC architecture. The views are defined in .fxml files, and those files also specify what controller classes are responsible for handling events triggered by components declared in the view.

I didn't include the MainController class, because it is literally empty. Just write a class that contains methods that handle events triggered by components declared in the .fxml file. You can give components an ID in the .fxml file, and then inject them into your controller class using the @FXML annotation. For details I suggest you follow a JavaFX tutorial, because I don't use it often enough that it will be faster in answering your questions than it will be for you to look it up yourself.
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.  I was just trying to get your code to work in case any other OPs were to try.
I am not a JavaFX programmer.
 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's something rudimentary:

It's good that you made me write this, because I remembered that because @FXML uses reflection to inject components into the controller, you need to open your application module for reflection:
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again.  It works.  I now have a working modules/maven project.
 
Aaron Esteban
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maven throwing errors during the clean command - Eclipse/Maven.

I created a simple test project in Maven since I'm new to using it and trying to learn. I was preparing to set up a .jar file, but when I went to run a build and clean in the project, I keep getting these errors:





What exactly would be causing this or how do I fix this? Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not Maven related. It's Eclipse complaining that you have multiple logger implementations installed. If this problem bothers you and you need help solving it, start a separate topic.

What does the rest of your build output say? Please post just the text output and no screenshots.
 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maven says Build Success. It only ran the 'clean' phase though. You need to run the 'verify' phase. What did you do to trigger Maven?
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic