1

It's been a few days since I've been trying to export a very simple project made in JavaFX but I have a very frustrating problem: To run the jar I need to open cmd navigate to the jdk folder and execute the following code java --module-path %path_to_JavaFX_on_my_pc% --add modules=javafx.controls,javafx.fxml,javafx.graphics -jar %path_to_jar% where I point to the jfx folder on the pc and add the necessary modules to run the jar.

run the jar using java java -jar %path_to_jar% results in the following error: Error: JavaFX runtime components are missing, and are required to run this application

The project is modular, having declared module-info.java with the following code:

module Timer {
requires java.prefs;
requires com.jfoenix;
requires javafx.base;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires javafx.media;
requires javafx.web;
requires javafx.swing;
opens main;
exports main;
}

when exporting the artifact I include all the .jar contained in the javafx, so why should I point to it externally?

image

The app runs well when I run it through the IDE, I didn't even have to add VM options.

My goal is to create an application that can actually be distributed, without the user needing to have any knowledge beyond the basics to run it, no jre, jdk, jfx, cmd code, etc... just click twice and done.

The question is: How do I generate an executable file that can be opened with 2 clicks like any other application on the pc on *any pc?

Gilian Marques
  • 417
  • 4
  • 16
  • Did you see the existing answers like here: https://stackoverflow.com/questions/30145772/what-is-the-best-way-to-deploy-javafx-application-create-jar-and-self-contained ? – CrazyCoder Mar 29 '20 at 23:07
  • I'm looking for the simplest possible way to do this, my project does not use gradle or maven so JavaPackager seemed the best option but this tool seems to be extinct ... there are some software that can convert my project into a .jar, .exe executable or any other that runs on windows? Actually I'm not sure how to proceed, as described, I can create a .jar but it is only executed by cmd using the command --add-modules .... so use a tool to convert this jar into another type of executable does not seem to solve the problem – Gilian Marques Mar 30 '20 at 01:11
  • You can use exe4j or launch4j and supply the required options via the configuration of these tools. But you are right, there is no easy way. JavaFX is hard to package, especially if you are not using Maven or Gradle. – CrazyCoder Mar 30 '20 at 01:13
  • I've already used Launch4J with java Swing it worked very well. But i'm not sure about how to proceed here, any tips? – Gilian Marques Mar 30 '20 at 01:23
  • "JavaPackager [...] seems to be extinct" – if using Java 14 take a look at `jpackage`. – Slaw Mar 30 '20 at 06:43
  • Also check out the questions I linked in [this other comment](https://stackoverflow.com/questions/60893053/javafx-10-or-older-for-download-to-create-a-runnable-jar-file#comment107738141_60893053). – Slaw Mar 30 '20 at 06:45
  • No jdk/jre is not possible. You need to create a installer. for that. Build a executable Jar, ship it with the runtime environment. Use launch4J to tell the exec where to locate the JavaRuntime and build the exe. Also you need buildscript, wich packages the Modules into the jar. – Luxusproblem Mar 30 '20 at 06:47

2 Answers2

0

finally I got a solution to my problem.

1 ° - It was necessary to solve the problem when generating a java artifact using javaFx in intelliJ: In JDK 13 the IDE threw the following error Can't build artifact - fx: deploy is not available in this JDK the easiest solution for that was to return on JDK 9 ond the javaFx was still built in and everything worked fine. Having done that, I was able to generate .jar artifacts that worked without the need to use command line tools.

2 ° -So I needed to generate a native executable for my application: In this topic there is an excellent list of tools that create launchers for java artifacts (Ideal was to convert but there gets a little complicated). What worked best for me was Jsmooth where I was able to set up a launcher that built in my .jar and where I could also attach a copy of the JRE for distribution on computers without Java

It is worth noting that I develop desktop applications just for my use and that of some friends, they do not work with sensitive data and do not require a high level of security and therefore there is no problem using an old version of jdk, in any other case, no recommend this approach.

Thank you all for your help.

Gilian Marques
  • 417
  • 4
  • 16
0

I ran into the same problem with JavaFX 11. The way I did it, to be able to generate the jar artifact, I set the Project Settings - Artifacts - Type to JAR rather than JavaFX Application. That enabled me to create a jar in the out directory of my project. Afterwards, I wrote a batch file that created a custom jre for my app (as small as ~40 MB for a small app), including JavaFX. I called that bat file create.bat and placed that bat file in the same folder as my jar artifact.

Now, provided

  • my jar artifact is called app.jar,
  • path to JDK is D:\jdks\jdk11,
  • path to JFX mods is D:\jdks\jfx11\jmods,
  • module name is com.epsilon, and
  • path to Main class is com.epsilon.Main,

below is the contents of the bat file to create a custom JRE, including JavaFX. It created a custom JRE in the folder dist, the launch file is in the dist\bin directory called run.bat.

rem This sets the variable DIR to the current directory with the jar artifact
set DIR=%~dp0

rem This creates a temporary mod file
D:\jdks\jdk11\bin\jmod create --class-path %DIR%app.jar %DIR%temp.mod

rem This creates distributable JRE
D:\jdks\jdk11\bin\jlink ^
--compress=2 ^
--strip-debug ^
--no-man-pages ^
--launcher run=com.epsilon/com.epsilon.Main ^
--module-path D:\jdks\jdk11\jmods;D:\jdks\jfx11\jmods;%DIR% ^
--add-modules com.epsilon ^
--output %DIR%dist

rem This command deletes the temporary mod file
del %DIR%temp.mod

rem You can create a shortcut to your app above the "dist" folder and enter the below line to the shortcut's target property
rem %windir%\system32\cmd.exe /c start "" "%CD%\dist\bin\javaw.exe" -m com.epsilon/com.epsilon.Main

So, this has enabled me to create a working distributable without downgrading Java.

Eugene Kartoyev
  • 501
  • 4
  • 11