-
Notifications
You must be signed in to change notification settings - Fork 25
/
ide-vscode.html
575 lines (407 loc) · 26.5 KB
/
ide-vscode.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
<h2>JavaFX and Visual Studio Code</h2>
<p>
This section explains how to create a JavaFX application in Visual Studio Code.
JavaFX, Visual Studio Code and some extensions were used for the screenshots.
The following list shows their versions.
</p>
<ul>
<li>JavaFX: 17.0.1</li>
<li>Visual Studio Code: 1.63.2 </li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack" target="_blank">Extension Pack for Java: 0.21.0</a></li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-gradle" target="_blank">Gradle for Java: 3.10.0</a></li>
</ul>
<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>
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="VSCode-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/VSCode/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>
<h5>1. Create a Java project</h5>
<p>
Select <kbd>Help -> Show All Commands</kbd>, in the dropdown list, enter <kbd>Java: Create Java project...</kbd>, and select it.
<a href="images/ide/vscode/ide/vscode01.png" target="_blank"><img src="images/ide/vscode/ide/vscode01.png" alt="Create a Java project"/></a>
Select <kbd>No build tools</kbd> in the list as the project type.
<a href="images/ide/vscode/ide/vscode02.png" target="_blank"><img src="images/ide/vscode/ide/vscode02.png" alt="Create a project without build tools"/></a>
Then select the target location you want to store the new project in the file explorer.
Provide a name to the project, like <kbd>HelloFX</kbd>.
<a href="images/ide/vscode/ide/vscode03.png" target="_blank"><img src="images/ide/vscode/ide/vscode03.png" alt="Input project name"/></a>
The new project will be opened in a new Visual Studio Code window.
You can open the <kbd>App.java</kbd> in the <kbd>src/</kbd> to activate the Java extensions and import the project.
</p>
<h5>2. Add JavaFX dependencies</h5>
<p>
To add JavaFX as dependencies to your project, you can simply copy all the jar files from the lib folder of your downloaded JavaFX SDK, for instance
<kbd>/Users/your-user/Downloads/javafx-sdk-<span class="JFX_VERSION">11</span>/lib/</kbd>
to the <kbd>lib</kbd> folder of your project.
Or alternatively, you can add them via the <kbd>Java Projects</kbd> explorer. Click the <kbd>+</kbd> button beside the <kbd>Referenced Library\ies</kbd>,
select the JavaFX library jars to add them.
<a href="images/ide/vscode/ide/vscode04.png" target="_blank"><img src="images/ide/vscode/ide/vscode04.png" alt="Add referenced libraries"/></a>
</p>
<h5>3.Add JavaFX classes</h5>
<p>
Remove the existing <kbd>App.java</kbd>. Create a new folder under <kbd>src/</kbd> and name it as <kbd>hellofx</kbd>. You can add the main class <kbd>App</kbd> in the newly created folder, based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/VSCode/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/VSCode/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/VSCode/Non-Modular/Java/hellofx/src/hellofx/Controller.java" target="_blank">controller</a>.
<a href="images/ide/vscode/ide/vscode05.png" target="_blank"><img src="images/ide/vscode/ide/vscode05.png" alt="Update the main class"/></a>
</p>
<h5>4. Create and update launch configurations</h5>
<p>
At the left side, switch to the <kbd>Run and Debug</kbd> viewlet, click <kbd>create a launch.json file</kbd>.
<a href="images/ide/vscode/ide/vscode06.png" target="_blank"><img src="images/ide/vscode/ide/vscode06.png" alt="Create a launch.json file"/></a>
Or you can directly create a <kbd>launch.json</kbd> file in the <kbd>.vscode</kbd> folder based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/VSCode/Non-Modular/Java/hellofx/.vscode/launch.json" target="_blank">one</a>.
Replace <kbd><JavaFX_lib_path></kbd> to the location lib folder of your downloaded JavaFX SDK.
<a href="images/ide/vscode/ide/vscode07.png" target="_blank"><img src="images/ide/vscode/ide/vscode07.png" alt="Update launch.json"/></a>
</p>
<h5>5. Run the project</h5>
<p>
In the <kbd>Run and Debug</kbd> viewlet, click the green run button to launch the application.
<a href="images/ide/vscode/ide/vscode08.png" target="_blank"><img src="images/ide/vscode/ide/vscode08.png" alt="Run the application"/></a>
</p>
<div id="VSCode-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/VSCode/Non-Modular/Maven" target="_blank">here</a>.
Make sure you have the <a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack" target="_blank">Extension Pack for Java</a> installed, which includs the required extensions to support Maven projects.
</p>
<h5>1. Create a Maven project</h5>
<p>
Select <kbd>Help -> Show All Commands</kbd>, in the dropdown list, enter <kbd>Java: Create Java project...</kbd>, and select it.
<a href="images/ide/vscode/maven/vscode01.png" target="_blank"><img src="images/ide/vscode/maven/vscode01.png" alt="Create a Java project"/></a>
Select <kbd>JavaFX</kbd> in the list as the project type.
<a href="images/ide/vscode/maven/vscode02.png" target="_blank"><img src="images/ide/vscode/maven/vscode02.png" alt="Create a JavaFX project"/></a>
Input gruop id, like <kbd>org.openjfx</kbd> and artifact id, like <kbd>hellofx</kbd> and select the project location in the file explorer.
Then a command will be sent and executed in the terminal, which creates a JavaFX project via the Maven Archetype for you.
Click <kbd>Open</kbd> button, the new project will be opened in a new Visual Studio Code window.
<a href="images/ide/vscode/maven/vscode03.png" target="_blank"><img src="images/ide/vscode/maven/vscode03.png" alt="Open the project"/></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/VSCode/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.App</kbd>.
And you can set the dependencies version to <kbd><span class="JFX_VERSION">17.0.1</span></kbd>, and set the <kbd>javafx-maven-plugin</kbd> version to <kbd><span class="JFX_MVN_PLUGIN_VERSION">0.0.8</span></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/vscode/maven/vscode04.png" target="_blank"><img src="images/ide/vscode/maven/vscode04.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/Vscode/Non-Modular/Maven/hellofx/src/main/java/org/openjfx/hellofx/App.java" target="_blank">App</a> main class:
<a href="images/ide/vscode/maven/vscode05.png" target="_blank"><img src="images/ide/vscode/maven/vscode05.png" alt="Verify the code"/></a>
</p>
<h5>4. Run the project</h5>
<p>
To run the project, open the <kbd>Maven</kbd> explorer, click <kbd><YOUR_PROJECT_NAME> -> Plugins -> javafx -> javafx:run</kbd>
<a href="images/ide/vscode/maven/vscode06.png" target="_blank"><img src="images/ide/vscode/maven/vscode06.png" alt="Run the project"/></a>
Alternatively, you can also open a terminal and run <kbd>mvn clean javafx:run</kbd> to run the project.
</p>
<div id="VSCode-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/VSCode/Non-Modular/Gradle" target="_blank">here</a>.
Make sure you have the <a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack" target="_blank">Extension Pack for Java</a> and <a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-gradle" target="_blank">Gradle for Java</a> installed, which includs the required extensions to support Gradle projects.
</p>
<h5>1. Create a Gradle project</h5>
<p>
Select <kbd>Help -> Show All Commands</kbd>, in the dropdown list, enter <kbd>Java: Create Java project...</kbd>, and select it.
<a href="images/ide/vscode/gradle/vscode01.png" target="_blank"><img src="images/ide/vscode/gradle/vscode01.png" alt="Create a Java project"/></a>
Select <kbd>Gradle</kbd> in the list as the project type.
<a href="images/ide/vscode/gradle/vscode02.png" target="_blank"><img src="images/ide/vscode/gradle/vscode02.png" alt="Create a Gradle project"/></a>
Select the project location in the file explorer and the build script DSL. Input the project name.
Then the <kbd>Gradle for Java</kbd> extension will create a new Gradle project for you.
The new project will be opened in a new Visual Studio Code window after the creation finished.
<a href="images/ide/vscode/gradle/vscode03.png" target="_blank"><img src="images/ide/vscode/gradle/vscode03.png" alt="Open the project"/></a>
Remove the <kbd>app/src/test</kbd> folder if you don't want to add tests for the project.
</p>
<h5>2. Modify the build</h5>
<p>
Edit the <kbd>build.gradle</kbd> file under the <kbd>app/</kbd> folder and replace it with this
<a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/VSCode/Non-Modular/Gradle/hellofx/build.gradle" target="_blank">build</a> file, setting the
<kbd>mainClassName</kbd> accordingly to <kbd>org.openjfx.MainApp</kbd>.
Make sure the plugin <kbd>org.openjfx.javafxplugin</kbd> is applied and the version is set to <kbd><span class="JFX_PLUGIN_VERSION">0.0.10</span></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">17.0.1</span>"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
</code></pre>
<p>
Right click on the <kbd>settings.gradle</kbd> and select <kbd>Update Project</kbd> to update the gradle project.
<a href="images/ide/vscode/gradle/vscode04.png" target="_blank"><img src="images/ide/vscode/gradle/vscode04.png" alt="Update the project"/></a>
Visual Studio Code will list all the JavaFX dependencies after the update.
<a href="images/ide/vscode/gradle/vscode05.png" target="_blank"><img src="images/ide/vscode/gradle/vscode05.png" alt="Gradle dependencies"/></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>
Remove <kbd>App.java</kbd> and its package. Based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/VSCode/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/VSCode/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/VSCode/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/VSCode/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/vscode/gradle/vscode06.png" target="_blank"><img src="images/ide/vscode/gradle/vscode06.png" alt="Add the source code"/></a>
</p>
<h5>4. Run the project</h5>
<p>
You can open the <kbd>Gradle</kbd> viewlet and click on <kbd><YOUR_PROJECT_NAME> -> build -> build</kbd> to
build the project, and <kbd><YOUR_PROJECT_NAME> -> application -> run</kbd> to run the project.
<a href="images/ide/vscode/gradle/vscode07.png" target="_blank"><img src="images/ide/vscode/gradle/vscode07.png" alt="Build and run"/></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-vscode-non-gradle-1" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-vscode-non-gradle-1" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-vscode-non-gradle-1">
<pre class="no-border-radius"><code>
./gradlew run
</code></pre>
</div>
<div class="tab-pane" id="win-vscode-non-gradle-1">
<pre><code>
.\gradlew.bat run
</code></pre>
</div>
</div>
<h3>Modular projects</h3>
<div id="VSCode-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/VSCode/Modular/Maven" target="_blank">here</a>.
Make sure you have the <a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack" target="_blank">Extension Pack for Java</a> installed, which includs the required extensions to support Maven projects.
</p>
<h5>1. Create a Maven project</h5>
<p>
Select <kbd>Help -> Show All Commands</kbd>, in the dropdown list, enter <kbd>Java: Create Java project...</kbd>, and select it.
<a href="images/ide/vscode/modular/maven/vscode01.png" target="_blank"><img src="images/ide/vscode/modular/maven/vscode01.png" alt="Create a Java project"/></a>
Select <kbd>JavaFX</kbd> in the list as the project type.
<a href="images/ide/vscode/modular/maven/vscode02.png" target="_blank"><img src="images/ide/vscode/modular/maven/vscode02.png" alt="Create a JavaFX project"/></a>
Input gruop id, like <kbd>org.openjfx</kbd> and artifact id, like <kbd>hellofx</kbd> and select the project location in the file explorer.
Then a command will be sent and executed in the terminal, which creates a JavaFX project via the Maven Archetype for you.
Click <kbd>Open</kbd> button, the new project will be opened in a new Visual Studio Code window.
<a href="images/ide/vscode/modular/maven/vscode03.png" target="_blank"><img src="images/ide/vscode/modular/maven/vscode03.png" alt="Open the project"/></a>
</p>
<h5>2. Verify the pom</h5>
<p>
Edit the <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/VSCode/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.App</kbd>.
And you can set the dependencies version to <kbd><span class="JFX_VERSION">17.0.1</span></kbd>, and set the <kbd>javafx-maven-plugin</kbd> version to <kbd><span class="JFX_MVN_PLUGIN_VERSION">0.0.8</span></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/vscode/modular/maven/vscode04.png" target="_blank"><img src="images/ide/vscode/modular/maven/vscode04.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/Vscode/Modular/Maven/hellofx/src/main/java/org/openjfx/hellofx/App.java" target="_blank">App</a> main class:
<a href="images/ide/vscode/modular/maven/vscode05.png" target="_blank"><img src="images/ide/vscode/modular/maven/vscode05.png" alt="Verify the code"/></a>
</p>
<h5>4. Run the project</h5>
<p>
To run the project, open the <kbd>Maven</kbd> explorer, click <kbd><YOUR_PROJECT_NAME> -> Plugins -> javafx -> javafx:run</kbd>
<a href="images/ide/vscode/modular/maven/vscode06.png" target="_blank"><img src="images/ide/vscode/modular/maven/vscode06.png" alt="Run the project"/></a>
Alternatively, you can also open a terminal and run <kbd>mvn clean javafx:run</kbd> to run the project.
</p>
<h5>6. Create a custom runtime image</h5>
<p>
To create a custom runtime, open the <kbd>Maven</kbd> explorer, click <kbd><YOUR_PROJECT_NAME> -> Plugins -> javafx -> javafx:jlink</kbd>
or run from a terminal with <kbd>JAVA_HOME</kbd> set:
</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-vscode-mod-mav-1" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-vscode-mod-mav-1" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-vscode-mod-mav-1">
<pre class="no-border-radius"><code>
target/hellofx/bin/launcher
</code></pre>
</div>
<div class="tab-pane" id="win-vscode-mod-mav-1">
<pre><code>
target\hellofx\bin\launcher.bat
</code></pre>
</div>
</div>
<div id="VSCode-Mod-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/VSCode/Modular/Gradle" target="_blank">here</a>.
Make sure you have the <a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack" target="_blank">Extension Pack for Java</a> and <a href="https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-gradle" target="_blank">Gradle for Java</a> installed, which includs the required extensions to support Gradle projects.
</p>
<h5>1. Create a Gradle project</h5>
<p>
Select <kbd>Help -> Show All Commands</kbd>, in the dropdown list, enter <kbd>Java: Create Java project...</kbd>, and select it.
<a href="images/ide/vscode/modular/gradle/vscode01.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode01.png" alt="Create a Java project"/></a>
Select <kbd>Gradle</kbd> in the list as the project type.
<a href="images/ide/vscode/modular/gradle/vscode02.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode02.png" alt="Create a Gradle project"/></a>
Select the project location in the file explorer and the build script DSL. Input the project name.
Then the <kbd>Gradle for Java</kbd> extension will create a new Gradle project for you.
The new project will be opened in a new Visual Studio Code window after the creation finished.
<a href="images/ide/vscode/modular/gradle/vscode03.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode03.png" alt="Open the project"/></a>
Remove the <kbd>app/src/test</kbd> folder if you don't want to add tests for the project.
</p>
<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/VSCode/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">17.0.1</span>"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
</code></pre>
<p>
Right click on the <kbd>settings.gradle</kbd> and select <kbd>Update Project</kbd> to update the gradle project.
<a href="images/ide/vscode/modular/gradle/vscode04.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode04.png" alt="Update the project"/></a>
Visual Studio Code will list all the JavaFX dependencies after the update.
<a href="images/ide/vscode/modular/gradle/vscode05.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode05.png" alt="Gradle dependencies"/></a>
As for any other Gradle dependencies, these jars can be found in the local <kbd>.gradle</kbd> repository.
</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>.
<a href="images/ide/vscode/modular/gradle/vscode06.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode06.png" alt="module-info"/></a>
</p>
<h5>4. Add the source code</h5>
<p>
Remove <kbd>App.java</kbd> and its package. Based on this <a class="samples" href="https://github.com/openjfx/samples/blob/master/IDE/VSCode/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/VSCode/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/VSCode/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/VSCode/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/vscode/modular/gradle/vscode07.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode07.png" alt="Add the source code"/></a>
</p>
<h5>5. Run the project</h5>
<p>
You can open the <kbd>Gradle</kbd> viewlet and click on <kbd><YOUR_PROJECT_NAME> -> build -> build</kbd> to
build the project, and <kbd><YOUR_PROJECT_NAME> -> application -> run</kbd> to run the project.
<a href="images/ide/vscode/modular/gradle/vscode08.png" target="_blank"><img src="images/ide/vscode/modular/gradle/vscode08.png" alt="Build and run"/></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-vscode-mod-gradle-1" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-vscode-mod-gradle-1" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-vscode-mod-gradle-1">
<pre class="no-border-radius"><code>
./gradlew run
</code></pre>
</div>
<div class="tab-pane" id="win-vscode-mod-gradle-1">
<pre><code>
.\gradlew.bat run
</code></pre>
</div>
</div>
<h5>6. 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 '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 {
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'hellofx'
}
}
</code></pre>
<p>
Then open the <kbd>Gradle</kbd> viewlet and click on <kbd><YOUR_PROJECT_NAME> -> 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-vscode-mod-gra-2" data-toggle="tab">Linux/Mac</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#win-vscode-mod-gra-2" data-toggle="tab">Windows</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="nix-vscode-mod-gra-2">
<pre class="no-border-radius"><code>
app/build/image/bin/hellofx
</code></pre>
</div>
<div class="tab-pane" id="win-vscode-mod-gra-2">
<pre><code>
app\build\image\bin\hellofx.bat
</code></pre>
</div>
</div>