EDIT #3 (SOLUTION)
I have solved this issue using none of the proposed answers. At the end of this I have some folders and files that I can zip and send around, that do not require a java installation and that can be run by double-clicking a .bat-file. The following procedure worked for me:
Create a new gradle project from scratch
Add all modules/code into the src/main/java folder
create an AppLauncher.java that calls the main-method
move all fxml/css from inside one of the java-folders to the resources-folder
make sure your calls to fxml-/css-files are not being refactored erroneously and use the following code:
//Main start method
Parent root = FXMLLoader.load(new File("fxml/main.fxml").toURI().toURL());
//Other FXML loading operations
FXMLLoader loader = null;
try { loader = new FXMLLoader(new File("fxml/GRAPHISO_proof.fxml").toURI().toURL()); }
...
and for stylesheets:
scene.getStylesheets().add("file:///" + new File("css/text-field-red-border.css").getAbsolutePath().replace('\\', '/'));
- create
module-info.java
inside of src/main/java folder with this structure:
module XYZ {
requires javafx.controls;
requires javafx.graphics;
requires java.desktop;
requires javafx.fxml;
requires javafx.base;
opens main.controller to javafx.fxml;
//Open ALL controller-modules (all of my modules have a controller-submodule, e.g. main/controller, dlog/controller, graphiso/controller, ...) to javafx.fxml
//...
exports main;
}
make sure gradle/wrapper/gradle-wrapper.properties is set to a not-so-old version of gradle (
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
)use this as your build.gradle:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'org.openjfx:javafx-plugin:0.0.8'
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
}
}
plugins {
id 'java'
id 'org.beryx.jlink' version '2.19.0'
}
apply plugin: 'org.openjfx.javafxplugin'
apply plugin: 'com.github.johnrengelman.shadow'
group 'org.example.abc'
version '1.0'
sourceCompatibility = 1.14
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
javafx {
sdk = 'C:\\Program Files\\Java\\javafx-sdk-14.0.1'
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.base' ]
}
jlink {
mainClassName = 'main.AppLauncher'
launcher {
name = 'Whatever you want as name'
}
}
tasks.jlink.doLast {
copy {
from('src/main/resources')
into("$buildDir/image/bin")
}
copy {
from 'C:\\Program Files\\Java\\javafx-sdk-14.0.1\\bin\\glass.dll',
'C:\\Program Files\\Java\\javafx-sdk-14.0.1\\bin\\javafx_font.dll',
'C:\\Program Files\\Java\\javafx-sdk-14.0.1\\bin\\prism_d3d.dll',
'C:\\Program Files\\Java\\javafx-sdk-14.0.1\\bin\\prism_sw.dll'
into("$buildDir/image/bin")
}
}
Note how I included some dll-files in tasks.jlink.doLast. Without them, the program keeps crashing and refers to the QuantumRenderer.
- install openjdk-14.0.1 binaries and set your %JAVA_HOME% accordingly, also install openjfx-14.0.1 (program needs some DLLs as mentioned above)
Original Question
I am trying to create a runtime image from a project that I have implemented in JavaFX 11 by just using the "run configuration" functionality in IntelliJ. It seems to me a big problem here is that my knowledge of maven/gradle are almost non-existent and that those build tools are crucial for this step.
Inside of C:\Program Files\Java
I've installed:
javafx-sdk-11.0.2
andjdk-13.0.2
I configured the VM options inside IntellJ's "run configuration" to be --module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" --add-modules javafx.controls,javafx.fxml
and set the Main Class to "main.Main".
My project structure looks like this: Project folder structure
The highlighted Main.java is an usual Main-file (extending Application, calling launch(args) inside of main). It can load other windows located in the other packages. It is not a Launcher-file that doesn't extend Application and calls the actual main-file (as suggested by a few solutions for gradle-building).
Initially I've tried to get a .jar-file that can be run without any further parameters on Windows and Linux. After some reading I figured that this wouldn't be a clean solution and a lot of sources stated that in modern solutions the .jar-files will be put together and shipped with a .bat/.sh file that includes all necessary parameters.
My question is: How do I best proceed and which configurations/additional files do I need to transform this "click-to-run-inside-IDE" project into an "standalone-application" that I can share through e-mail or similar?
I have tried to create a new gradle project through IntelliJ (following https://openjfx.io/openjfx-docs) and copy-pasted the source files into src/main/java. I then tried out the provided build.gradle-file and - after that didn't work - made some changes suggested by various blogs/forums/etc. None of them allowed me to run the project through IntelliJ or from the command line.
Most of these approaches let me build the project but when trying to run using either "gradlew run" or calling the "java -jar file.jar" I got error messages due to class exceptions or saying I am still missing the javafx modules. I've also tried an approach to convert my current project into a gradle-project but that wouldn't even let me build the project afterwards.
EDIT #1 (JPackageScriptFX)
I have looked at the JPackageScriptFX-link. Now I am getting an installer (using mvn clean install
) that works on my machine, but when running the resulting executable, I receive an error message that says "Failed to launch JVM".
What I have done so far is:
installed jdk-14.0.1 and set JAVA_HOME, installed wix311
created a new IntelliJ maven project called "bachelor_maven"
removed initial src-folder
added new module "ZKP_Inspector" and my old modules inside of the resulting src/main/java folder
added and edited src/main/logo/windows/duke.ico, the two pom.xml files and build_app.bat from JPackageScriptFX
implemented AppLauncher.java that calls Main.java
My configuration files:
New project structure: Imgur
pom.xml (Project level, bachelor_maven): Pastebin
pom.xml (module level, ZKP_Inspector): Pastebin
build_app.bat: Pastebin
EDIT #2
I tried removing --runtime-image target/java-runtime ^
from the jpackage parameters in the build_app.bat file as to let jpackage decide what to include. This increased the file size of the installer (as expected) but lead to the same error when trying to execute the resulting program.
I have also downloaded the JMOD files and used them in the jlink task in the build_app.bat as hinted here.
So I replaced --add-modules %detected_modules%,%manual_modules% ^
with
--module-path "C:\Program Files\Java\javafx-jmods-11.0.2" --add-modules %detected_modules%,%manual_modules%,javafx.controls,javafx.fxml
This produced another installer that ran fine and whose resulting program threw the same error.