Until you run Hello World of JavaFX with VS Code + Gradle

Introduction

Make a note of how to run JavaFX "for the time being" with VSCode + Gradle. It was made by copying the wisdom of our predecessors everywhere. Thank you to all our ancestors.

0. Environment

The tools and execution environment used are as follows.

Also, since we will focus on the title part, we will omit the installation of individual tools.

1. Create a project in Gradle

Create a project directory and run gradle init (assuming you're in your path to the gradle command). It will be completed if you answer the options as follows.

ximia@thinkpad:~/dev/sandbox02$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Swift
Enter selection (default: Java) [1..5] 3

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Select test framework:
  1: JUnit 4 VSCode
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 1

Project name (default: sandbox02): 
Source package (default: sandbox02): me.osa2.java.sandbox02

> Task :init
Get more help with your project: https://docs.gradle.org/6.1.1/userguide/tutorial_java_projects.html

BUILD SUCCESSFUL in 42s
2 actionable tasks: 2 executed
ximia@thinkpad:~/dev/sandbox02$ 

2. Add tasks.json in VS Code

Add the sandbox02 directory created above to the VS Code workspace and then run the default build. However, VS Code does not know what to do and asks me to create a Config (see the figure below). Click "Configure Build Task ..." displayed in the center of the screen (If you have translated VS Code into Japanese, please translate it in your brain as appropriate).

Screenshot at 2020-02-16 18-51-30.png

Select Other from the options. Then tasks.json that just displays Hello will be created under the .vscode folder of the project. Copy and paste the following completely over there.

{
    "version": "1.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "./gradlew build"
        },
        {
            "label": "run",
            "type": "shell",
            "command": "./gradlew run",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "./gradlew clean"
        },
        {
            "label": "check",
            "type": "shell",
            "command": "./gradlew check"
        },
        {
            "label": "create-jar",
            "type": "shell",
            "command": "./gradlew jar"
        }
    ]
}

After saving, select Terminal> Run Buld Task ... from the VS Code menu, the build will run slowly on the shell screen below, and "Hello World" in the console app that Gradle prepared at the time of init will be displayed. If you look at tasks.json, you can see that gradle run is the default for build tasks.

3. Replace with JavaFX code

It's very crude, but in VSCode, overwrite src / main / java / (package path) /App.java completely with the following code and rename the file to HelloWorld.java. Also, the test skeleton under src / test will also cause an error (because I will not write the test code here), so delete it.

package your.package.path;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class HelloWorld extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

In addition, this code has been borrowed from the following page of Oracle in the world. https://docs.oracle.com/javafx/2/get_started/hello_world.htm

4. Pass the classpath for VSCode and Gradle respectively

First, replace build.gradle completely as follows and save it. Please rewrite the mainClassName according to your convenience. If you build (gradle run) up to this point, the JavaFX application will be launched. The image of the app is at the end.

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.1.1/userguide/tutorial_java_projects.html
 */

plugins {
    id 'java'
    id 'application'
    id 'eclipse' // for auto-completion enabled .classpath.
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

repositories {
    jcenter()
}

javafx {
    version = "11"
    modules = [ 'javafx.controls']
}

dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:28.1-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    // Making Runnable Jar
    implementation "com.github.jengelman.gradle.plugins:shadow:5.1.0"
}

mainClassName = 'me.osa2.java.sandbox02.HelloWorld'

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

jar {
        manifest {
        attributes ('Main-Class': mainClassName, "Implementation-Title": "Gradle",
                "Implementation-Version": 1)
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    from configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}

Next, create a launch.json file in the .vscode folder (the folder where tasks.json was created earlier), and paste the following as the contents. Please rewrite the main class name, package name, and openjfx lib path according to your convenience. Now when you restart VS Code, the red wave underline of the identifier that was unrecognizable on the source should disappear.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch) - Current File",
            "request": "launch",
            "mainClass": "${file}"
        },
        {
            "type": "java",
            "name": "Debug (Launch)-HelloWorld<sandbox02>",
            "request": "launch",
            "mainClass": "me.osa2.java.sandbox02.HelloWOrld",
            "projectName": "hello",
            "vmArgs": "--module-path /home/ximia/libs/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml"
        }
    ]
}

5. Conclusion

It's a collection of other Qiita articles, and it's just "Marutto Copipe". app.png Is it good for people who want to think about things after moving? I hope you get used to it! : grinning:

== END ==

Recommended Posts

Until you run Hello World of JavaFX with VS Code + Gradle
Hello World with VS Code!
Using Gradle with VS Code, build Java → run
Until you install Gradle and output "Hello World"
Hello World with SpringBoot / Gradle
Until "Hello World" with Spring Boot
Hello world with Kotlin and JavaFX
Java development with Codenvy: Hello World! Run
Hello World on Mac VS Code Java
(IntelliJ + gradle) Hello World with Spring Boot
Hello World with JavaFX 11 (OpenJFX) in Liberica JDK 11
Run JSP Hello World with Tomcat on Docker
Until you run the Apache Velocity sample code
Try to display hello world with spring + gradle
Profile-derived automation of Code Artifact authentication with Gradle
Lombok with VS Code
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Build ruby debug environment with VS Code of Windows 10
Introduce JavaFX 15 and do GUI development with VS Code
The procedure I did when I prepared the environment of gradle + Java with VS Code (Windows 10)
Create Restapi with Spring Boot ((1) Until Run of App)
Hello World with Micronaut
Hello World with Spring Boot
Docker management with VS Code
Hello World with Spring Boot!
Format Ruby with VS Code
java hello world, compile, run
Hello World with Spring Boot
Hello, World! With Asakusa Framework!
Summary of steps for developing in Docker container with VS Code
Try Hello World with the minimum configuration of Heroku Java spring-boot
Spring Boot programming with VS Code
Hello World with Docker and C
Java build with mac vs code
(Intellij) Hello World with Spring Boot
Hello World with GlassFish 5.1 + Servlet + JSP
Create PDF with itext7 ~ Hello World ~
Getting Started with Docker with VS Code
"Hello world" for ImageJ with Eclipse
Hello World with GWT 2.8.2 and Maven
Hello world in Java and Gradle
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Try writing "Hello, World" with a combination of various languages and libraries