-
Notifications
You must be signed in to change notification settings - Fork 25
/
modular.html
798 lines (683 loc) · 26.5 KB
/
modular.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
<h2>Runtime images</h2>
<p>
A runtime image of your JavaFX project is a custom JRE that contains only the platform modules that are required for your application.
</p>
<p>
If you want to create a runtime image of your JavaFX project follow these instructions.
</p>
<p>
Download an appropriate <a target="_blank" href="https://gluonhq.com/products/javafx/">JavaFX runtime and JavaFX jmods</a>
for your operating system and unzip them to a desired location.
</p>
<p>
Add these environment variables pointing to the lib directory of the runtime and to the jmods directory:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-mods" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-mods" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-mods">
<pre class="no-border-radius"><code>
export PATH_TO_FX=path/to/javafx-sdk-<span class="JFX_VERSION">12</span>/lib
export PATH_TO_FX_MODS=path/to/javafx-jmods-<span class="JFX_VERSION">11</span>
</code></pre>
</div>
<div class="tab-pane" id="win-mods">
<pre><code>
set PATH_TO_FX="path\to\javafx-sdk-<span class="JFX_VERSION">12</span>\lib"
set PATH_TO_FX_MODS="path\to\javafx-jmods-<span class="JFX_VERSION">11</span>"
</code></pre>
</div>
</div>
<div id="MOD-CLI"></div><h3>Command Line</h3>
<p>
You can run or create a runtime image of your JavaFX project from command line. A simple example can be found in this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/CLI" target="_blank">project</a>.
</p>
<p>
The application has a single class <a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/CLI/hellofx/src/hellofx/HelloFX.java" target="_blank">HelloFX.java</a> with its
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/CLI/hellofx/src/module-info.java" target="_blank">module-info</a>
file that defines the <kbd>hellofx</kbd> module.
Compile it either using the JavaFX SDK:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-javacd" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-javacd" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-javacd">
<pre class="no-border-radius"><code>
javac --module-path $PATH_TO_FX -d mods/hellofx $(find src/ -name "*.java")
</code></pre>
</div>
<div class="tab-pane" id="win-javacd">
<pre><code>
dir /s /b src\*.java > sources.txt & javac --module-path %PATH_TO_FX% -d mods/hellofx @sources.txt & del sources.txt
</code></pre>
</div>
</div>
<p>
or using the JavaFX jmods:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-javacdmod" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-javacdmod" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-javacdmod">
<pre class="no-border-radius"><code>
javac --module-path $PATH_TO_FX_MODS -d mods/hellofx $(find src/ -name "*.java")
</code></pre>
</div>
<div class="tab-pane" id="win-javacdmod">
<pre><code>
dir /s /b src\*.java > sources.txt & javac --module-path %PATH_TO_FX_MODS% -d mods/hellofx @sources.txt & del sources.txt
</code></pre>
</div>
</div>
<div class="alert alert-warning">
<strong>Note: </strong>
Note that the output is placed under <kbd>mods/hellofx</kbd> for convenience, but it could use any other name.
</div>
<p>
To run the modular application with the <kbd>java</kbd> command you need the JavaFX SDK modules and your module
added to the module path:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-javamod" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-javamod" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-javamod">
<pre class="no-border-radius"><code>
java --module-path $PATH_TO_FX:mods -m hellofx/hellofx.HelloFX
</code></pre>
</div>
<div class="tab-pane" id="win-javamod">
<pre><code>
java --module-path "%PATH_TO_FX%;mods" -m hellofx/hellofx.HelloFX
</code></pre>
</div>
</div>
<a class="jlink"></a><h4>jlink</h4>
<p>
With a modular project, <a href="https://docs.oracle.com/en/java/javase/15/docs/specs/man/jlink.html" target="_blank">jlink</a>
can be used to create a custom runtime image using the JavaFX jmods:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-jlink" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-jlink" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-jlink">
<pre class="no-border-radius"><code>
$JAVA_HOME/bin/jlink --module-path $PATH_TO_FX_MODS:mods --add-modules hellofx --output hellofx
</code></pre>
</div>
<div class="tab-pane" id="win-jlink">
<pre><code>
jlink --module-path "%PATH_TO_FX_MODS%;mods" --add-modules hellofx --output hellofx
</code></pre>
</div>
</div>
<p>
And after the image is built, you can run it:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-run" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-run" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-run">
<pre class="no-border-radius"><code>
hellofx/bin/java -m hellofx/hellofx.HelloFX
</code></pre>
</div>
<div class="tab-pane" id="win-run">
<pre><code>
hellofx\bin\java -m hellofx/hellofx.HelloFX
</code></pre>
</div>
</div>
<div class="alert alert-warning">
<strong>Note: </strong>
This custom JRE is platform-specific.
</div>
<div id="MOD-Maven"></div><h3>Maven</h3>
<p>
You can run or create a runtime of your JavaFX Maven project. A simple example can be found in this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Maven" target="_blank">project</a>.
</p>
<p>
The application has a main class <a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Maven/hellofx/src/main/java/org/openjfx/MainApp.java" target="_blank">MainApp.java</a> with its
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Maven/hellofx/src/main/java/module-info.java" target="_blank">module-info</a>
file that defines the <kbd>hellofx</kbd> module. It also uses FXML, and requires the <kbd>javafx.fxml</kbd> dependency added to
the <a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Maven/hellofx/pom.xml" target="_blank">pom.xml</a>.
</p>
<p>
It can be compiled and run on command line with <kbd>javac</kbd>, but using Maven we can simply do:
</p>
<pre><code>
mvn clean javafx:run
</code></pre>
<p>
To create a custom runtime, using the <a href="https://github.com/openjfx/javafx-maven-plugin" target="_blank">JavaFX Maven plugin</a>, you can do:
</p>
<pre><code>
mvn clean javafx:jlink
</code></pre>
<p>
Note the plugin allows the usual <a href="https://github.com/openjfx/javafx-maven-plugin#javafxjlink-options" target="_blank">options</a> as the <kbd>jlink</kbd> command, as well as creating a
launcher or a zip with the custom image.
</p>
<p>
And after the image is built, you can run it from command line:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-mvn-run" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-mvn-run" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-mvn-run">
<pre class="no-border-radius"><code>
target/hellofx/bin/launcher
</code></pre>
</div>
<div class="tab-pane" id="win-mvn-run">
<pre><code>
target\hellofx\bin\launcher
</code></pre>
</div>
</div>
<div id="MOD-Gradle"></div><h3>Gradle</h3>
<p>
You can run or create a runtime of your JavaFX Gradle project. A simple example can be found in this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Gradle" target="_blank">project</a>.
</p>
<p>
The application has a main class <a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Gradle/hellofx/src/main/java/hellofx/HelloFX.java" target="_blank">HelloFX.java</a> with its
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Gradle/hellofx/src/main/java/module-info.java" target="_blank">module-info</a>
file that defines the <kbd>hellofx</kbd> module, and the required
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Modular/Gradle/hellofx/build.gradle" target="_blank">build.gradle</a> file.
</p>
<p>
It can be compiled and run on command line with <kbd>javac</kbd>, but using Gradle we can simply do:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-cligradlerun" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-cligradlerun" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-cligradlerun">
<pre class="no-border-radius"><code>
./gradlew run
</code></pre>
</div>
<div class="tab-pane" id="win-cligradlerun">
<pre><code>
gradlew run
</code></pre>
</div>
</div>
<p>
To create a custom runtime the referred <a class="modular-jlink-action" href="#">steps</a> can be used to generate a gradle task.
Alternatively, there is a plugin that does it for us: the <a href="https://badass-jlink-plugin.beryx.org/releases/latest/" target="_blank">org.beryx.jlink</a>.
It can be combined with the JavaFX Gradle plugin:
</p>
<pre><code>
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '<span class="JFX_PLUGIN_VERSION">0.0.7</span>'
id 'org.beryx.jlink' version '<span class="JLINK_PLUGIN_VERSION">2.9.4</span>'
}
javafx {
version = "<span class="JFX_VERSION">12</span>"
modules = [ 'javafx.controls' ]
}
jlink {
launcher {
name = 'hellofx'
}
}
</code></pre>
<p>
to generate and run the custom image:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-cligradlelink" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-cligradlelink" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-cligradlelink">
<pre class="no-border-radius"><code>
./gradlew jlink
build/image/bin/hellofx
</code></pre>
</div>
<div class="tab-pane" id="win-cligradlelink">
<pre><code>
gradlew jlink
build\image\bin\hellofx
</code></pre>
</div>
</div>
<div id="JDK-FX"></div><h3>Custom JDK+JavaFX image</h3>
<p>
You can use <kbd>jlink</kbd> to create a runtime image that includes some or all the JavaFX modules,
without being attached to a given project.
</p>
<p>
With this image you will be able to run JavaFX modular or non-modular projects, use it as a new JRE from your
IDE to create regular JavaFX projects, or even use its <kbd>jlink</kbd> command to create a new
custom image for your project.
</p>
<p>
For instance, you can create an image that contains the <kbd>java.se</kbd>
<a href="https://docs.oracle.com/en/java/javase/15/docs/api/java.se/module-summary.html" target="_blank">module</a> and the JavaFX modules,
by running:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-jdkfx" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-jdkfx" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-jdkfx">
<pre class="no-border-radius"><code>
export PATH_TO_FX_MODS=path/to/javafx-jmods-<span class="JFX_VERSION">11</span>
$JAVA_HOME/bin/jlink --module-path $PATH_TO_FX_MODS \
--add-modules java.se,javafx.fxml,javafx.web,javafx.media,javafx.swing \
--bind-services --output /path/to/jdkfx-<span class="JFX_VERSION">11</span>.jdk
</code></pre>
</div>
<div class="tab-pane" id="win-jdkfx">
<pre><code>
set PATH_TO_FX_MODS="path\to\javafx-jmods-<span class="JFX_VERSION">11</span>"
%JAVA_HOME%\bin\jlink --module-path %PATH_TO_FX_MODS% \
--add-modules java.se,javafx.fxml,javafx.web,javafx.media,javafx.swing \
--bind-services --output \path\to\jdkfx-<span class="JFX_VERSION">11</span>
</code></pre>
</div>
</div>
<h4>Use custom image</h4>
<p>
With this custom image, you can set a new <kbd>JAVA_HOME</kbd>:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-jdkfx-home" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-jdkfx-home" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-jdkfx-home">
<pre class="no-border-radius"><code>
export JAVA_HOME=/path/to/jdkfx-<span class="JFX_VERSION">11</span>.jdk
</code></pre>
</div>
<div class="tab-pane" id="win-jdkfx-home">
<pre><code>
set JAVA_HOME="path\to\jdkfx-<span class="JFX_VERSION">11</span>"
</code></pre>
</div>
</div>
<p>
Since the JavaFX modules are already part of the runtime, now you can run the HelloFX <a href="" target="_blank">sample</a>
without the need of adding <kbd>PATH_TO_FX</kbd> anymore, like:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-jdkfx-run" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-jdkfx-run" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-jdkfx-run">
<pre class="no-border-radius"><code>
javac -d mods/hellofx $(find src/ -name "*.java")
java --module-path mods -m hellofx/hellofx.HelloFX
</code></pre>
</div>
<div class="tab-pane" id="win-jdkfx-run">
<pre><code>
dir /s /b src\*.java > sources.txt & javac -d mods/hellofx @sources.txt & del sources.txt
java --module-path mods -m hellofx/hellofx.HelloFX
</code></pre>
</div>
</div>
<p>
Or add the image to your IDE:
<a href="images/modular/netbeans01.png" target="_blank"><img src="images/modular/netbeans01.png" alt="Adding JDK+FX platform"/></a>
And start creating Java projects with JavaFX classes.
<a href="images/modular/netbeans02.png" target="_blank"><img src="images/modular/netbeans02.png" alt="Create JavaFX project"/></a>
And run them without any additional option:
<a href="images/modular/netbeans03.png" target="_blank"><img src="images/modular/netbeans03.png" alt="Run JavaFX project"/></a>
</p>
<div class="alert alert-warning">
<strong>Note: </strong>
This custom JRE is platform-specific and it is not intended for distribution, and if there is a new JDK or a new
JavaFX SDK, it has to be created all over again. However, its <kbd>jlink</kbd> tool can be used to create a custom
image with the project that can be distributed.
And the <kbd><a href="https://docs.oracle.com/en/java/javase/15/docs/specs/man/jpackage.html" target="_blank">jpackage tool</a></kbd> can
be used for distributing installers with the jlinked project.
</div>
<div id="Non-Mod"></div><h3>Non-modular application</h3>
<p>
Since Java 9, applications should be modular, and distributed with tools like <a href="index.html#modular">jlink</a>.
However, if you have a non-modular JavaFX project or you can't use <kbd>jlink</kbd> because you
have non-modular dependencies where the automatic module naming convention doesn't apply, you can still create a fat jar.
</p>
<p>
As explained <a href="http://mail.openjdk.java.net/pipermail/openjfx-dev/2018-June/021977.html" target="_blank">here</a>,
in order to create a runnable jar with all the required JavaFX dependencies,
you will need to use a launcher class that doesn't extend from <kbd>Application</kbd>.
</p>
<h4>Maven</h4>
<p>
If you develop your JavaFX applications using Maven, you don't have to download
the JavaFX SDK. Just specify the modules and the versions you want in the <kbd>pom.xml</kbd> file,
and the build system will download the required modules, including the native libraries
for your platform.
</p>
<p>
A simple Maven example can be found in this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Non-modular/Maven" target="_blank">project</a>.
The <a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Non-modular/Maven/hellofx/pom.xml" target="_blank">pom.xml</a>
file shows how to create a runnable fat jar for this <kbd>hellofx</kbd> project with the Maven shade plugin.
</p>
<p>
Run the plugin to create the fat jar:
</p>
<pre><code>
mvn compile package
</code></pre>
<p>
Run the application using:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-maven" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-maven" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-maven">
<pre class="no-border-radius"><code>
java -jar shade/hellofx.jar
</code></pre>
</div>
<div class="tab-pane" id="win-maven">
<pre><code>
java -jar shade\hellofx.jar
</code></pre>
</div>
</div>
<h5>Cross-platform jar</h5>
<p>
You can create a cross-platform fat jar by adding the dependencies from the
three platforms that require native libraries to your pom file, in this sample
only the ones from <kbd>javafx.graphics</kbd>:
</p>
<pre><code>
<dependencies>
...
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version><span class="JFX_VERSION">12</span></version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version><span class="JFX_VERSION">12</span></version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version><span class="JFX_VERSION">12</span></version>
<classifier>mac</classifier>
</dependency>
</dependencies>
</code></pre>
<h4>Gradle</h4>
<p>
If you develop your JavaFX applications using Gradle, you don't have to download
the JavaFX SDK. Just specify the modules and the versions you want in the <kbd>build.gradle</kbd> file,
and the build system will download the required modules,
including the native libraries for your platform. However, for Gradle we need to
find and specify the platform/OS as classifier.
</p>
<p>
A simple Gradle example can be found in this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Non-modular/Gradle" target="_blank">project</a>.
In order to create a runnable fat jar for this project with Gradle,
modify the <kbd>jar</kbd> task in the <a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Non-modular/Gradle/hellofx/build.gradle" target="_blank">build</a>
file to include the launcher class:
</p>
<pre><code>
mainClassName = 'hellofx.HelloFX'
jar {
manifest {
attributes 'Main-Class': 'hellofx.Launcher'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
</code></pre>
<p>
Now run the task to create the fat jar and run it:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-gradle" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-gradle" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-gradle">
<pre class="no-border-radius"><code>
./gradlew jar
java -jar build/libs/hellofx.jar
</code></pre>
</div>
<div class="tab-pane" id="win-gradle">
<pre><code>
gradlew.bat jar
java -jar build\libs\hellofx.jar
</code></pre>
</div>
</div>
<h5>Cross-platform jar</h5>
<p>
You can create a cross-platform fat jar by adding the dependencies from the
three platforms that require native libraries to the build file, in this sample
only the ones from <kbd>javafx.graphics</kbd>:
</p>
<pre><code>
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:win"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:linux"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:mac"
</code></pre>
<p>
Now run the jar task again to create the cross-platform fat jar.
</p>
<a name="command-line"></a><h3>Command Line</h3>
<p>
Finally, you can also create a runnable fat jar for your JavaFX project on command line.
</p>
<div class="alert alert-danger">
<strong>Warning</strong>: This is a discouraged tedious error-prone manual process
that should be avoided by using the <a href="#Maven" class="alert-danger">Maven</a>'s shade plugin or
the <a href="#Gradle" class="alert-danger">Gradle</a>'s jar task, in case <a href="index.html#modular" class="alert-danger">jlink</a>
is not applicable.
</div>
<p>
An example can be found in this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/CommandLine/Non-modular/CLI" target="_blank">project</a>.
Download the appropriate
<a target="_blank" href="https://gluonhq.com/products/javafx/">JavaFX SDK</a>
for your operating system and unzip it to a desired location.
</p>
<p>
Add an environment variable pointing to the lib directory of the runtime:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-fxpath" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-fxpath" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-fxpath">
<pre class="no-border-radius"><code>
export PATH_TO_FX=/path/to/javafx-sdk-<span class="JFX_VERSION">11</span>/lib
</code></pre>
</div>
<div class="tab-pane" id="win-fxpath">
<pre><code>
set PATH_TO_FX="path\to\javafx-sdk-<span class="JFX_VERSION">11</span>\lib"
</code></pre>
</div>
</div>
<p>
Compile the project:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-javacout" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-javacout" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-javacout">
<pre class="no-border-radius"><code>
javac --module-path $PATH_TO_FX --add-modules=javafx.controls -d out $(find src/main/java -name "*.java")
</code></pre>
</div>
<div class="tab-pane" id="win-javacout">
<pre><code>
dir /s /b src\main\java\*.java > sources.txt & javac --module-path %PATH_TO_FX% --add-modules=javafx.controls -d out @sources.txt & del sources.txt
</code></pre>
</div>
</div>
<p>
And create the fat jar, adding the JavaFX required jars and the platform-specific native libraries. For
the <kbd>hellofx</kbd> project:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-jar" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-jar" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-jar">
<pre class="no-border-radius"><code>
find $PATH_TO_FX/{javafx.base.jar,javafx.graphics.jar,javafx.controls.jar} -exec unzip -nq {} -d out \;
#uncomment for Linux:
#cp $PATH_TO_FX/{libprism*.so,libjavafx*.so,libglass*.so,libdecora_sse.so} out
#uncomment for Mac:
#cp $PATH_TO_FX/{libprism*.dylib,libjavafx*.dylib,libglass.dylib,libdecora_sse.dylib} out
rm out/META-INF/MANIFEST.MF out/module-info.class
mkdir libs
jar --create --file=libs/hellofx.jar --main-class=hellofx.Launcher -C out .
</code></pre>
</div>
<div class="tab-pane" id="win-jar">
<pre><code>
cd out & jar xf "%PATH_TO_FX%\javafx.base.jar" & jar xf "%PATH_TO_FX%\javafx.graphics.jar" & jar xf "%PATH_TO_FX%\javafx.controls.jar" & cd ..
copy "%PATH_TO_FX%\..\bin\prism*.dll" out & copy "%PATH_TO_FX%\..\bin\javafx*.dll" out & copy "%PATH_TO_FX%\..\bin\glass.dll" out && copy "%PATH_TO_FX%\..\bin\decora_sse.dll" out
del out\META-INF\MANIFEST.MF & del out\module-info.class
mkdir libs
jar --create --file=libs/hellofx.jar --main-class=hellofx.Launcher -C out .
</code></pre>
</div>
</div>
<p>
And after the jar is built, you can run it:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-jarrun" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-jarrun" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-jarrun">
<pre class="no-border-radius"><code>
java -jar libs/hellofx.jar
</code></pre>
</div>
<div class="tab-pane" id="win-jarrun">
<pre><code>
java -jar libs\hellofx.jar
</code></pre>
</div>
</div>
<div class="alert alert-warning">
<strong>Note: </strong>
This JRE is platform-specific. However it can be easily a multi-platform jar if the
different native libraries from each platform were included. This requires downloading the JavaFX SDKs
for all the platforms, extracting all the necessary jars (for instance, <kbd>javafx.graphics</kbd> is
different for each platform), and copying all the native libraries as shown above. As it was mentioned
above, this process shouldn't be done manually.
</div>