-
Notifications
You must be signed in to change notification settings - Fork 25
/
ide-eclipse.html
859 lines (653 loc) · 37.1 KB
/
ide-eclipse.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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
<h2>JavaFX and Eclipse</h2>
<p>
This section explains how to create a JavaFX application in Eclipse.
JavaFX 15.0.1 and Eclipse 2020-12 (4.18.0) were used for the IDE screenshots.
</p>
<p>
Download an appropriate JDK for your operating system and set <kbd>JAVA_HOME</kbd> to the JDK directory.
Refer to <a href="#install-java">Install Java</a> section for more information.
</p>
<p>
Include the new JDK as <kbd>Installed JREs</kbd> in <kbd>Eclipse -> Window -> Preferences -> Java -> Installed JREs -> Add</kbd>.
</p>
<p>
You can create a JavaFX modular or non-modular project and use the IDE tools, Maven or Gradle build tools.
</p>
<h3>Non-modular projects</h3>
<div id="ECLIPSE-IDE"></div><h4>IDE</h4>
<p>
Follow these steps to create a JavaFX non-modular project and use the IDE tools to build it and run it.
Alternatively, you can download a similar project from <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Java" target="_blank">here</a>.
</p>
<p>
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, for instance
<kbd>/Users/your-user/Downloads/javafx-sdk-<span class="JFX_VERSION">11</span></kbd>.
</p>
<p>
Create a new <kbd>User Library</kbd> under <kbd>Eclipse -> Window -> Preferences -> Java -> Build Path -> User Libraries -> New</kbd>.
<a href="images/ide/eclipse/ide/eclipse01.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse01.png" alt="Add User Library"/></a>
Name it <kbd>JavaFX<span class="JFX_MAJOR">11</span></kbd> and include the jars under the <kbd>lib</kbd> folder from JavaFX <span class="JFX_MAJOR">11</span>.
<a href="images/ide/eclipse/ide/eclipse02.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse02.png" alt="Add JavaFX jars"/></a>
</p>
<h5>1. Create a Java project</h5>
<p>
Select <kbd>File -> New -> Java Project</kbd>, and provide a name to the project, like <kbd>HelloFX</kbd>, and a location.
<a href="images/ide/eclipse/ide/eclipse03.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse03.png" alt="Create a Java project"/></a>
Make sure JDK 11 or greater is selected.
You don't need to add a <kbd>module-info.java</kbd> file. You can include the <kbd>JavaFX<span class="JFX_VERSION">11</span></kbd> library into the classpath.
<a href="images/ide/eclipse/ide/eclipse04.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse04.png" alt="Add JavaFX11 library"/></a>
An empty project will be opened.
</p>
<h5>2. Add JavaFX classes</h5>
<p>
You can add a main class <kbd>Main</kbd>, based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Java/hellofx/src/hellofx/Main.java" target="_blank">one</a>,
with an <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Java/hellofx/src/hellofx/hellofx.fxml" target="_blank">FXML</a> file and
a <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Java/hellofx/src/hellofx/Controller.java" target="_blank">controller</a>.
<a href="images/ide/eclipse/ide/eclipse05.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse05.png" alt="Add JavaFX project"/></a>
</p>
<div class="alert alert-danger">
<strong>Warning: </strong>
If you now run the project it will compile but you will get this error:
<pre><code>
Error: JavaFX runtime components are missing, and are required to run this application
</code></pre>
This error is shown since the Java <span class="JDK_MAJOR">11</span> launcher checks if the main class extends
<kbd>javafx.application.Application</kbd>. If that is the case, it is required to
have the <kbd>javafx.graphics</kbd> module on the module-path.
</div>
<h5>3. Add VM arguments</h5>
<p>
To solve the issue, click on <kbd>Run -> Run Configurations... -> Java Application</kbd>,
create a new launch configuration for your project named `hellofx` and add these VM arguments:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-eclipse-non-ide-1" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-eclipse-non-ide-1" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-eclipse-non-ide-1">
<pre class="no-border-radius"><code>
--module-path /path/to/javafx-sdk-<span class="JFX_VERSION">11</span>/lib --add-modules javafx.controls,javafx.fxml
</code></pre>
</div>
<div class="tab-pane" id="win-eclipse-non-ide-1">
<pre><code>
--module-path "\path\to\javafx-sdk-<span class="JFX_VERSION">11</span>\lib" --add-modules javafx.controls,javafx.fxml
</code></pre>
</div>
</div>
<div class="alert alert-warning">
<strong>Warning: </strong>
Make sure the checkbox <b>"Use the -XstartOnFirstThread argument when launching with SWT"</b> is not checked.
</div>
<p>
<a href="images/ide/eclipse/ide/eclipse06.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse06.png" alt="VM arguments"/></a>
Click apply and close the dialog.
</p>
<h5>4. Run the project</h5>
<p>
Click <kbd>Run -> Run As -> Java Application -> Main - hellofx</kbd> to run the project.
</p>
<h5>Environment variable</h5>
<p>
You can replace the module path with the environment variable <kbd>PATH_TO_FX</kbd> if you add the path to
<kbd>Eclipse -> Preferences -> Run/Debug -> String Substitution -> New...</kbd>.
<a href="images/ide/eclipse/ide/eclipse07.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse07.png" alt="String Substitution"/></a>
This will let you use <kbd>${PATH_TO_FX}</kbd> as vm argument:
<a href="images/ide/eclipse/ide/eclipse08.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse08.png" alt="New VM arguments"/></a>
And you can distribute the project or reuse it in other projects too.
</p>
<h5>E(fx)clipse</h5>
<p>
If you use the <a href="http://www.eclipse.org/efxclipse/index.html">e(fx)clipse</a> plugin,
make sure you are running at least the version 3.6.0 that can be installed using from the Eclipse Marketplace.
<a href="images/ide/eclipse/ide/eclipse09.png" target="_blank"><img src="images/ide/eclipse/ide/eclipse09.png" alt="E(fx)clipse"/></a>
Note that you will have to do exactly the same as in the regular Java project above, including the <kbd>JavaFX<span class="JFX_VERSION">11</span></kbd> library
and adding the necessary VM arguments.
</p>
<div id="ECLIPSE-Maven"></div><h4>Maven</h4>
<p>
Follow these steps to create a JavaFX non-modular project and use the Maven tools to build it and run it.
Alternatively, you can download a similar project from <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Maven" target="_blank">here</a>.
Make sure you have the Maven Integration for Eclipse m2e plugin installed, but
Eclipse 2019-03 already ships by default the latest version.
</p>
<h5>1. Create a Maven project</h5>
<p>
Create a <kbd>File -> New -> Project... -> Maven -> Maven project</kbd>.
<a href="images/ide/eclipse/maven/eclipse00.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse00.png" alt="Create a Maven project"/></a>
The first time you will need to add the JavaFX archetypes: Select <kbd>Add Archetypes...</kbd> and type: <kbd>org.openjfx</kbd> for the group id,
<kbd>javafx-archetype-simple</kbd> or <kbd>javafx-archetype-fxml</kbd> for the artifact id, and <kbd><span class="JFX_MVN_ARCH_VERSION">0.0.1</span></kbd> for the version.
<a href="images/ide/eclipse/maven/eclipse01.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse01.png" alt="Add JavaFX archetypes"/></a>
Type <kbd>org.openjfx</kbd> in the filter field and select the archetype, between <kbd>javafx-archetype-fxml</kbd> or <kbd>javafx-archetype-simple</kbd>, based on the use
of FXML or not in your project.
<a href="images/ide/eclipse/maven/eclipse02.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse02.png" alt="JavaFX Maven archetypes"/></a>
Provide the groupId, like <kbd>org.openjfx</kbd>, the artifactId, like <kbd>hellofx</kbd>.
You can edit the <kbd>javafx-version</kbd> property, and set it to <span class="JFX_VERSION">11</span>,
and the plugin version to <span class="JFX_MVN_PLUGIN_VERSION">0.0.1</span>.
<a href="images/ide/eclipse/maven/eclipse03.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse03.png" alt="JavaFX FXML archetype"/></a>
When the project opens, select the JDK 11 or greater for the project (<kbd>File -> Properties -> Java Build Path -> Libraries</kbd>).
<a href="images/ide/eclipse/maven/eclipse04.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse04.png" alt="Set JDK"/></a>
For a non-modular project, you can remove the <kbd>module-info.java</kbd> file.
</p>
<h5>2. Verify the pom</h5>
<p>
Edit the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Maven/hellofx/pom.xml" target="_blank">pom</a> file,
and and verify it has the <kbd>javafx.controls</kbd> and <kbd>javafx.fxml</kbd> dependencies and the <kbd>javafx-maven-plugin</kbd> with
the <kbd>mainClass</kbd> set to <kbd>org.openjfx.hellofx.App</kbd>.
Maven manages the required dependencies: it will add <kbd>javafx.base</kbd> and <kbd>javafx.graphics</kbd> that are required by
<kbd>javafx.controls</kbd> and <kbd>javafx.fxml</kbd>, but most important, it will add the required classifier based on your platform, downloading
the specific platform jars.
<a href="images/ide/eclipse/maven/eclipse05.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse05.png" alt="Update the pom"/></a>
As for any other maven dependencies, these jars can be found in the local <kbd>.m2</kbd> repository.
</p>
<h5>3. Verify the code</h5>
<p>
Verify the project contains the source code files, like the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Maven/hellofx/src/main/java/org/openjfx/hellofx/App.java" target="_blank">App</a> main class:
<a href="images/ide/eclipse/maven/eclipse06.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse06.png" alt="Add JavaFX project"/></a>
</p>
<h5>4. Run the project</h5>
<p>
Click <kbd>Run -> Run As -> Maven Build -> New launch configuration</kbd> to create a new configuration.
Name it <kbd>hellofx</kbd>, and edit the required goals, adding:
</p>
<pre><code>
clean javafx:run
</code></pre>
<p>
<a href="images/ide/eclipse/maven/eclipse07.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse07.png" alt="Run project"/></a>
Run the project <kbd>Run -> Run As -> Maven Build -> hellofx -> Run</kbd>.
</p>
<div class="alert alert-warning">
<strong>Note: </strong>
In case <kbd>JAVA_HOME</kbd> is not set to <span class="JDK_MAJOR">11</span>, running the project might
fail. To avoid it, you can define <kbd>JAVA_HOME</kbd> as an environment variable (where <kbd>${JAVA_HOME}</kbd> takes the value that can be set in <kbd>Eclipse -> Preferences -> Run/Debug -> String Substitution -> New...</kbd>):
<a href="images/ide/eclipse/maven/eclipse08.png" target="_blank"><img src="images/ide/eclipse/maven/eclipse08.png" alt="Set Java Home"/></a>
Alternatively, you can add the correct <kbd>java</kbd> command to the javafx-maven-plugin:
<kbd><configuration><executable>/path/to/jdk-<span class="JDK_MAJOR">11</span>/bin/java</executable></configuration></kbd>.
</div>
<p>
You can also open a terminal and run <kbd>mvn clean javafx:run</kbd> to run the project.
</p>
<div class="alert alert-danger">
<strong>Warning: </strong>
You might find this exception when running your project:
<pre><code class="big">
Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError: <init>
at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens(Native Method)
at javafx.graphics/com.sun.glass.ui.Screen.initScreens(Screen.java:412)
...
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.graphics/com.sun.prism.d3d.D3DPipeline.getAdapterOrdinal(D3DPipeline.java:205)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.assignScreensAdapters(QuantumToolkit.java:695)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runToolkit(QuantumToolkit.java:313)
...
</code></pre>
This exception is thrown on Windows when Eclipse VM is set to Oracle JDK 1.8 using Maven or Gradle projects.
A workaround for this is to edit <kbd>eclipse.ini</kbd> and change the <kbd>-vm</kbd> option to JDK <span class="JDK_MAJOR">11</span>.
If that is not possible, another workaround is to add this VM argument to the javafx maven plugin:
<pre><code>
<configuration>
<options>
<option>-Djava.library.path=C:\tmp</option>
</options>
<mainClass>org.openjfx.hellofx.App</mainClass>
</configuration>
</code></pre>
</div>
<div id="ECLIPSE-Gradle"></div><h4>Gradle</h4>
<p>
Follow these steps to create a JavaFX project and use the Gradle tools to build it and run it.
Alternatively, you can download a similar project from <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Gradle" target="_blank">here</a>.
Make sure you have the Buildship Gradle Integration 3.0 plugin installed, which is bundled with Eclipse 2019-03
</p>
<h5>1. Create a Gradle project</h5>
<p>
Create a <kbd>File -> New -> Project... -> Gradle -> Gradle project</kbd>.
<a href="images/ide/eclipse/gradle/eclipse00.png" target="_blank"><img src="images/ide/eclipse/gradle/eclipse00.png" alt="Create a Gradle project"/></a>
Provide a name to the project, like <kbd>HelloFX</kbd> and a location for the project. Then point <kbd>JAVA_HOME</kbd> to JDK <span class="JDK_MAJOR">11</span>.
<a href="images/ide/eclipse/gradle/eclipse01.png" target="_blank"><img src="images/ide/eclipse/gradle/eclipse01.png" alt="Gradle project settings"/></a>
Then close the dialog, the project will be created.
<a href="images/ide/eclipse/gradle/eclipse02.png" target="_blank"><img src="images/ide/eclipse/gradle/eclipse02.png" alt="Open project"/></a>
</p>
<div class="alert alert-warning">
<strong>Note: </strong>
Instead of setting the Java home in the gradle dialog, you can add
the property <kbd>org.gradle.java.home</kbd> to a <kbd>gradle.properties</kbd> file,
with the correct path to JDK <span class="JDK_MAJOR">11</span>. This file can be part of the project or under
the gradle user home <kbd>$USER_HOME/.gradle</kbd>.
</div>
<h5>2. Modify the build</h5>
<p>
Edit the <kbd>build.gradle</kbd> file and replace it with this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Gradle/hellofx/build.gradle" target="_blank">build</a> file, setting the
<kbd>mainClassName</kbd> accordingly to <kbd>org.openjfx.MainApp</kbd>.
</p>
<p>
Similar to Maven, we can declare the required JavaFX modules in the <kbd>build.gradle</kbd> file.
However, for Gradle we need to apply the JavaFX gradle plugin:
</p>
<pre><code>
javafx {
version = "<span class="JFX_VERSION">12</span>"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
</code></pre>
<p>
Refresh the gradle project and you will get the JavaFX dependencies.
<a href="images/ide/eclipse/gradle/eclipse03.png" target="_blank"><img src="images/ide/eclipse/gradle/eclipse03.png" alt="Update the build"/></a>
As for any other Gradle dependencies, these jars can be found in the local <kbd>.gradle</kbd> repository.
</p>
<h5>3. Add the source code</h5>
<p>
Based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Gradle/hellofx/src/main/java/org/openjfx/MainApp.java" target="_blank">MainApp</a> class,
add its content to the project main class. Then add the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Gradle/hellofx/src/main/java/org/openjfx/FXMLController.java" target="_blank">controller</a>
and the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Gradle/hellofx/src/main/resources/org/openjfx/scene.fxml" target="_blank">FXML</a> and
the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Gradle/hellofx/src/main/resources/org/openjfx/styles.css" target="_blank">css</a>
files.
</p>
<p>
You can add a main class <kbd>MainApp</kbd>, and an <kbd>FXMLController</kbd> class, and add to resources the <kbd>FXML</kbd> file.
<a href="images/ide/eclipse/gradle/eclipse04.png" target="_blank"><img src="images/ide/eclipse/gradle/eclipse04.png" alt="Add JavaFX project"/></a>
</p>
<h5>4. Run the project</h5>
<p>
You can open the Gradle Task window and click on <kbd>build -> build</kbd> to
build the project, and <kbd>application -> run</kbd> to run the project. If this task is not visible, press the
<kbd>Refresh Tasks for all projects</kbd> button at the top-right of the Gradle Task window.
</p>
<p>
You can also click on <kbd>Run -> Run configurations... -> Gradle Project -> hellofx</kbd> to add a run task to the project.
<a href="images/ide/eclipse/gradle/eclipse05.png" target="_blank"><img src="images/ide/eclipse/gradle/eclipse05.png" alt="Run configuration"/></a>
</p>
<p>
You can also open a terminal and run:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-eclipse-non-gradle-1" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-eclipse-non-gradle-1" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-eclipse-non-gradle-1">
<pre class="no-border-radius"><code>
./gradlew run
</code></pre>
</div>
<div class="tab-pane" id="win-eclipse-non-gradle-1">
<pre><code>
gradlew run
</code></pre>
</div>
</div>
<div class="alert alert-danger">
<strong>Warning: </strong>
You might find this exception when running your project:
<pre><code class="big">
Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError: <init>
at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens(Native Method)
at javafx.graphics/com.sun.glass.ui.Screen.initScreens(Screen.java:412)
...
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.graphics/com.sun.prism.d3d.D3DPipeline.getAdapterOrdinal(D3DPipeline.java:205)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.assignScreensAdapters(QuantumToolkit.java:695)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runToolkit(QuantumToolkit.java:313)
...
</code></pre>
This is happening running on Windows when Eclipse VM is set to Oracle JDK 1.8 using Maven or Gradle projects.
A workaround for this is to edit <kbd>eclipse.ini</kbd> and change the <kbd>-vm</kbd> option to JDK <span class="JDK_MAJOR">11</span>.
If that is not possible, another workaround is to add this VM argument to the build file:
<pre><code>
run {
if (osdetector.os == 'windows') {
systemProperty "java.library.path", "C:\tmp"
}
}
</code></pre>
</div>
<h3>Modular projects</h3>
<p>
Download the appropriate
<a target="_blank" href="https://gluonhq.com/products/javafx/">JavaFX jmods</a>
for your operating system and unzip it to a desired location, for instance
<kbd>/Users/your-user/Downloads/javafx-jmods-<span class="JFX_MAJOR">11</span></kbd>.
</p>
<div id="ECLIPSE-Mod-IDE"></div><h4>IDE</h4>
<p>
Follow these steps to create a JavaFX modular project and use the IDE tools to build it and run it.
Alternatively, you can download a similar project from <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Java" target="_blank">here</a>.
</p>
<h5>1. Create a Java project</h5>
<p>
Select <kbd>File -> New -> Java Project</kbd> and provide a name to the project, like <kbd>HelloFX</kbd>, and a location.
Make sure JDK 11 or greater is selected.
Verify you have selected the option to create a <kbd>module-info.java</kbd> file, and
select the output folder as <kbd>HelloFX/bin/hellofx</kbd>.
</p>
<p>
You can add the <kbd>JavaFX<span class="JFX_MAJOR">11</span></kbd> library to the classpath. Then press <kbd>Finish</kbd>.
A dialog will ask for the name of the module, which should be set in lower case to <kbd>hellofx</kbd>.
Press <kbd>Create</kbd>. An empty project will be opened.
</p>
<p>
Create a package <kbd>org.openjfx</kbd> and add a main class <kbd>MainApp</kbd> that extends <kbd>Application</kbd>.
</p>
<h5>2. Edit the module-info class</h5>
<p>
Edit the <kbd>module-info</kbd> class, and include the required modules <kbd>javafx.controls</kbd> and <kbd>javafx.fxml</kbd>.
Since FXML uses reflection to access the controller in the module, this has to be opened to <kbd>javafx.fxml</kbd>. Finally,
export the package <kbd>org.openjfx</kbd>.
</p>
<div class="alert alert-warning">
<strong>Note: </strong>
Eclipse will show a warning when creating the <kbd>Application</kbd> class:
<pre><code>
The type Stage from module <kbd>javafx.graphics</kbd> may not be accessible to clients due to missing 'requires transitive'
</code></pre>
This warning can be prevented by adding the transitive module to the file:
</div>
<p>
<a href="images/ide/eclipse/modular/ide/eclipse01.png" target="_blank"><img src="images/ide/eclipse/modular/ide/eclipse01.png" alt="module-info"/></a>
</p>
<h5>3. Add JavaFX classes</h5>
<p>
Based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Java/HelloFX/src/org/openjfx/MainApp.java" target="_blank">MainApp</a> class,
add its content to the project main class. Then add the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Java/HelloFX/src/org/openjfx/FXMLController.java" target="_blank">controller</a>
and the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Java/HelloFX/src/org/openjfx/scene.fxml" target="_blank">FXML</a> and
the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Java/HelloFX/src/org/openjfx/styles.css" target="_blank">css</a>
files.
</p>
<h5>4. Add VM options</h5>
<p>
Being a modular project, and since we already added the <kbd>JavaFX<span class="JFX_MAJOR">11</span></kbd> library to the module-path,
there is no need to add any VM arguments.
</p>
<div class="alert alert-warning">
<strong>Warning: </strong>
Make sure the checkbox <b>"Use the -XstartOnFirstThread argument when launching with SWT"</b> is not checked.
</div>
<h5>5. Run the project</h5>
<p>
Click <kbd>Run -> Run Configurations... -> Java Application</kbd> to run the project.
</p>
<h5>6. Create a custom runtime image</h5>
<p>
To create a runtime image, run the following commands on a terminal:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-eclipse-mod-ide-2" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-eclipse-mod-ide-2" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-eclipse-mod-ide-2">
<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:bin/hellofx --add-modules=hellofx --output jre
jre/bin/java -m hellofx/org.openjfx.MainApp
</code></pre>
</div>
<div class="tab-pane" id="win-eclipse-mod-ide-2">
<pre><code>
set PATH_TO_FX_MODS="path\to\javafx-jmods-<span class="JFX_VERSION">11</span>"
jlink --module-path "%PATH_TO_FX_MODS%;bin\hellofx" --add-modules=hellofx --output jre
jre\bin\java -m hellofx/org.openjfx.MainApp
</code></pre>
</div>
</div>
<div id="ECLIPSE-Mod-Maven"></div><h4>Maven</h4>
<p>
Follow these steps to create a JavaFX modular project and use the Maven tools to build it and run it.
Alternatively, you can download a similar project from <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Maven" target="_blank">here</a>.
</p>
<h5>1. Create a Maven project</h5>
<p>
Create a <kbd>File -> New -> Project... -> Maven -> Maven project</kbd>.
The first time you will need to add the JavaFX archetypes: Select <kbd>Add Archetypes...</kbd> and type: <kbd>org.openjfx</kbd> for the group id,
<kbd>javafx-archetype-simple</kbd> or <kbd>javafx-archetype-fxml</kbd> for the artifact id, and <kbd><span class="JFX_MVN_ARCH_VERSION">0.0.1</span></kbd> for the version.
<a href="images/ide/eclipse/modular/maven/eclipse01.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse01.png" alt="Add JavaFX archetypes"/></a>
Type <kbd>org.openjfx</kbd> in the filter field and select the archetype, between <kbd>javafx-archetype-fxml</kbd> or <kbd>javafx-archetype-simple</kbd>, based on the use
of FXML or not in your project.
<a href="images/ide/eclipse/modular/maven/eclipse02.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse02.png" alt="JavaFX Maven archetypes"/></a>
Provide the groupId, like <kbd>org.openjfx</kbd>, the artifactId, like <kbd>hellofx</kbd>.
You can edit the <kbd>javafx-version</kbd> property, and set it to <span class="JFX_VERSION">11</span>,
and the plugin version to <span class="JFX_MVN_PLUGIN_VERSION">0.0.1</span>.
<a href="images/ide/eclipse/modular/maven/eclipse03.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse03.png" alt="JavaFX FXML archetype"/></a>
When the project opens, select the JDK <span class="JDK_MAJOR">11</span> for the project (<kbd>File -> Properties -> Java Build Path -> Libraries</kbd>).
<a href="images/ide/eclipse/maven/modular/eclipse04.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse04.png" alt="Set JDK"/></a>
<h5>2. Verify the pom</h5>
<p>
Edit the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Non-Modular/Maven/hellofx/pom.xml" target="_blank">pom</a> file,
and and verify it has the <kbd>javafx.controls</kbd> and <kbd>javafx.fxml</kbd> dependencies and the <kbd>javafx-maven-plugin</kbd> with
the <kbd>mainClass</kbd> set to <kbd>hellofx/org.openjfx.hellofx.App</kbd>.
<a href="images/ide/eclipse/modular/maven/eclipse05.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse05.png" alt="pom"/></a>
</p>
<h5>3. Verify the code</h5>
<p>
Edit the <kbd>module-info</kbd> class. Rename the module to <kbd>hellofx</kbd>. Also, to prevent Eclipse
from showing a warning when creating the <kbd>Application</kbd> class, add also the transitive module to the file:
<a href="images/ide/eclipse/modular/maven/eclipse06.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse06.png" alt="module-info"/></a>
</p>
<p>
Verify the project contains the source code files, like the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Maven/hellofx/src/main/java/org/openjfx/hellofx/App.java" target="_blank">App</a> main class:
<a href="images/ide/eclipse/modular/maven/eclipse07.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse07.png" alt="Source code"/></a>
</p>
<h5>5. Run the project</h5>
<p>
Click <kbd>Run -> Run As -> Maven Build -> New launch configuration</kbd> to create a new configuration.
Name it <kbd>hellofx</kbd>, and add the required goals:
</p>
<pre><code>
clean javafx:run
</code></pre>
<p>
Run the project <kbd>Run -> Run As -> Maven Build -> hellofx -> Run</kbd>.
</p>
<div class="alert alert-warning">
<strong>Note: </strong>
In case <kbd>JAVA_HOME</kbd> is not set to <span class="JDK_MAJOR">11</span>, running the project might
fail. To avoid it, you can define <kbd>JAVA_HOME</kbd> as an environment variable (where <kbd>${JAVA_HOME}</kbd> takes the value that can be set in <kbd>Eclipse -> Preferences -> Run/Debug -> String Substitution -> New...</kbd>):
<a href="images/ide/eclipse/modular/maven/eclipse08.png" target="_blank"><img src="images/ide/eclipse/modular/maven/eclipse08.png" alt="Set Java Home"/></a>
Alternatively, you can add the correct <kbd>java</kbd> command to the javafx-maven-plugin:
<kbd><configuration><executable>/path/to/jdk-<span class="JDK_MAJOR">11</span>/bin/java</executable></configuration></kbd>.
</div>
<p>
You can also open a terminal and run <kbd>mvn clean javafx:run</kbd> to run the project.
</p>
<div class="alert alert-danger">
<strong>Warning: </strong>
You might find this exception when running your project:
<pre><code class="big">
Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError: <init>
at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens(Native Method)
at javafx.graphics/com.sun.glass.ui.Screen.initScreens(Screen.java:412)
...
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.graphics/com.sun.prism.d3d.D3DPipeline.getAdapterOrdinal(D3DPipeline.java:205)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.assignScreensAdapters(QuantumToolkit.java:695)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runToolkit(QuantumToolkit.java:313)
...
</code></pre>
This is happening running on Windows when Eclipse VM is set to Oracle JDK 1.8 using Maven or Gradle projects.
A workaround for this is to edit <kbd>eclipse.ini</kbd> and change the <kbd>-vm</kbd> option to JDK <span class="JDK_MAJOR">11</span>.
If that is not possible, another workaround is to add this VM argument to the javafx maven plugin:
<pre><code>
<configuration>
<options>
<option>-Djava.library.path=C:\tmp</option>
</options>
<mainClass>org.openjfx.hellofx.App</mainClass>
</configuration>
</code></pre>
</div>
<h5>6. Create a custom runtime image</h5>
<p>
To create a custom runtime, you can edit the run configuration an modify the goals to <kbd>clean javafx:jlink</kbd>
or run from a terminal with <kbd>JAVA_HOME</kbd> set to <span class="JDK_MAJOR">11</span>:
</p>
<pre><code>
mvn clean javafx:jlink
</code></pre>
<p>
Note the plugin allows the usual options 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-eclipse-mod-mav-1" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-eclipse-mod-mav-1" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-eclipse-mod-mav-1">
<pre class="no-border-radius"><code>
target/hellofx/bin/launcher
</code></pre>
</div>
<div class="tab-pane" id="win-eclipse-mod-mav-1">
<pre><code>
target\hellofx\bin\launcher
</code></pre>
</div>
</div>
<div id="ECLIPSE-Mod-Gradle"></div><h4>Gradle</h4>
<p>
Follow these steps to create a JavaFX modular project and use the Gradle tools to build it and run it.
Alternatively, you can download a similar project from <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Gradle" target="_blank">here</a>.
</p>
<h5>1. Create a Gradle project</h5>
<p>
Create a <kbd>File -> New -> Project... -> Gradle -> Gradle project</kbd>.
Provide a name to the project, like <kbd>HelloFX</kbd> and a location for the project.
Then point Java Home to JDK <span class="JDK_MAJOR">11</span>.
Then close the dialog, the project will be created.
</p>
<div class="alert alert-warning">
<strong>Note: </strong>
Instead of setting the Java home in the gradle dialog, you can add
the property <kbd>org.gradle.java.home</kbd> to a <kbd>gradle.properties</kbd> file,
with the correct path to JDK <span class="JDK_MAJOR">11</span>. This file can be part of the project or under
the gradle user home <kbd>$USER_HOME/.gradle</kbd>.
</div>
<h5>2. Modify the build</h5>
<p>
Edit the <kbd>build.gradle</kbd> file and replace it with this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Gradle/hellofx/build.gradle" target="_blank">build</a> file, setting the
<kbd>mainClassName</kbd> accordingly to <kbd>org.openjfx.MainApp</kbd>.
</p>
<p>
Note the use of the <kbd>org.openjfx.javafxplugin</kbd> plugin, that removes the necessity of adding the
JavaFX dependencies and setting the module-path for the compile and run task for them.
</p>
<p>
Note also that the use of the <kbd>eclipse</kbd> plugin is required to include the modules to the module path.
<a href="images/ide/eclipse/modular/gradle/eclipse01.png" target="_blank"><img src="images/ide/eclipse/modular/gradle/eclipse01.png" alt="Update the build"/></a>
</p>
<h5>3. Add the module-info class</h5>
<p>
Add the <kbd>module-info</kbd> class, including the required modules <kbd>javafx.controls</kbd> and <kbd>javafx.fxml</kbd>.
Since FXML uses reflection to access the controller in the module, this has to be opened to <kbd>javafx.fxml</kbd>. Finally,
export the package <kbd>org.openjfx</kbd>.
To prevent Eclipse from showing a warning when creating the <kbd>Application</kbd> class, add also the transitive modules to the file:
<a href="images/ide/eclipse/modular/gradle/eclipse02.png" target="_blank"><img src="images/ide/eclipse/modular/gradle/eclipse02.png" alt="module-info"/></a>
</p>
<h5>3. Add the source code</h5>
<p>
Based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Gradle/hellofx/src/main/java/org/openjfx/MainApp.java" target="_blank">MainApp</a> class,
add its content to the project main class. Then add the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Gradle/hellofx/src/main/java/org/openjfx/FXMLController.java" target="_blank">controller</a>
and the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Gradle/hellofx/src/main/resources/org/openjfx/scene.fxml" target="_blank">FXML</a> and
and the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/Eclipse/Modular/Gradle/hellofx/src/main/resources/org/openjfx/styles.css" target="_blank">css</a>
files.
</p>
<h5>4. Run the project</h5>
<p>
You can open the Gradle Task window and click on <kbd>build -> build</kbd> to
build the project, and <kbd>application -> run</kbd> to run the project.
You can also click on <kbd>Run -> Run configurations... -> Gradle Project -> hellofx</kbd> to add a run task to the project.
</p>
<p>
You can also open a terminal and run:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-eclipse-mod-gra-1" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-eclipse-mod-gra-1" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-eclipse-mod-gra-1">
<pre class="no-border-radius"><code>
./gradlew run
</code></pre>
</div>
<div class="tab-pane" id="win-eclipse-mod-gra-1">
<pre><code>
gradlew run
</code></pre>
</div>
</div>
<h5>5. Create a custom runtime image</h5>
<p>
To create a custom runtime, you can use the <a href="https://badass-jlink-plugin.beryx.org/releases/latest/" target="_blank">org.beryx.jlink</a> plugin.
It can be easily combined with the JavaFX Gradle plugin.
</p>
<pre><code>
plugins {
id 'application'
id 'eclipse'
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', 'javafx.fxml' ]
}
jlink {
launcher {
name = 'hellofx'
}
}
</code></pre>
<p>
Then run <kbd>hellofx -> Tasks -> build -> jlink</kbd> task to create the image.
To run the image:
</p>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#nix-eclipse-mod-gra-2" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-eclipse-mod-gra-2" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-eclipse-mod-gra-2">
<pre class="no-border-radius"><code>
build/image/bin/hellofx
</code></pre>
</div>
<div class="tab-pane" id="win-eclipse-mod-gra-2">
<pre><code>
build\image\bin\hellofx
</code></pre>
</div>
</div>