Getting started with Java programs using Visual Studio Code

publickey's article "[Free code editor VS Code realizes" Hot Code Replacement "that can dynamically rewrite and modify the code of a running Java application" (http://www.publickey1.jp Inspired by "/blog/18/vscodejavahot_code_replacement.html)", I tried using VSCode.

environment

Preparation

Installation

Download / install Stable Build from https://code.visualstudio.com. Currently version 1.19.

Java development environment settings

--Open Extension (Ctrl + Shift + X) view --Install Java Extension Pack (Debugger for Java, Laguage Support for Java is also installed) screen0.png --Open the setting screen (Ctrl +,) and set * java.home * according to your environment.

Program execution

Hello, World! First, let's write Hello, World !. --File-Open Folder --Select a new file --Select Java in the language mode selection at the bottom right. Character code and line feed code are your choice screen1.png --It seems that * templates * such as main and sysout are prepared. screen2.png --Save As (Ctrl + Shift + S). * Classpath is incomplete. Only syntax errors will be reported * Ignore the warning for now. --You can run the program by starting debugging (F5), but the * launch.json * file will be created the first time, so run it again with F5. --The debug view opens as shown below, and the execution result is displayed on the console. screen3.png

Display of Japanese (correspondence to garbled characters)

Naturally, I would like to say that Japanese is displayed without any problem, but it was not so sweet. If you keep the default, the console display will be garbled as shown below.

screen4.png

To handle this, open the settings screen and add **-Dfile.encoding = utf8 ** to * java.jdt.ls.vmargs *. Since it is a JVM argument, I thought it would be okay to change the order and character code to * UTF-8 *, but even if I add **-Dfile.encoding = UTF-8 ** to the end of the original value It was no good. * "java.jdt.ls.vmargs": "-noverify -Dfile.encoding = utf8 -Xmx1G -XX: + UseG1GC -XX: + UseStringDeduplication" * The garbled characters could be resolved by adding. screen5.png

Hot Code Replacement Experiment

Change settings

Program modification

Modify the program as follows to try * Hot Code Replacement *.

Test.java


public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++)
            sayHello();
    }

    private static void sayHello() {
        System.out.println("Hello World!");
    }
}

Program execution

Set a breakpoint at the place where you call the * sayHello () * method and start debugging. --Stop at breakpoint -* sayHello () * Rewriting the method. Save --Continue (F5) By repeating, you can see that the processing of the * sayHello () * method has been replaced without restarting the program. screen6.png

Summary

Using VSCode, I was able to check the * Hot Code Replacement * of the Java program. Finally, the settings I added to develop Java with * VS Code * are as follows.

{
    "java.home": "Set the JDK path",
    "java.jdt.ls.vmargs": "-noverify  -Dfile.encoding=utf8 -Xmx1G -XX:+UseG1GC -XX:+UseStringDeduplication",
    "java.debug.settings.enableHotCodeReplace": true,
}

I'd like the console to handle garbled characters by default, but I'm glad that I could handle it by adding a little setting.

Functionally, it's not enough compared to the * Eclipse * that I usually use, but above all, the lightness of this operation is attractive. It is said that it also supports * Spring Boot *, so if you have a chance, try it.

Recommended Posts

Getting started with Java programs using Visual Studio Code
Getting Started with Java Collection
Java in Visual Studio Code
Getting Started with Java Basics
Why can I develop Java with Visual Studio Code?
Build Java program development environment with Visual Studio Code
Use PlantUML with Visual Studio Code
Getting started with Java lambda expressions
Getting Started with Docker with VS Code
Build WebAPP development environment with Java + Spring with Visual Studio Code
Getting Started with Ruby for Java Engineers
Getting Started with Java Starting from 0 Part 1
What I learned from doing Java work with Visual Studio Code
[Mac] Install Java in Visual Studio Code
A record of setting up a Java development environment with Visual Studio Code
Try remote debugging of Java with Remote Containers in Visual Studio Code Insiders
Experience .NET 5 with Docker and Visual Studio Code
Links & memos for getting started with Java (for myself)
Add --enable-preview option in Java in Visual Studio Code
Getting Started with Java 1 Putting together similar things
Using Gradle with VS Code, build Java → run
Getting started with Kotlin to send to Java developers
[Mac] Install Java in Visual Studio Code (VS Code)
Getting Started with DBUnit
Getting Started with Ruby
Getting Started with Swift
Getting Started with Docker
Getting Started with Doma-Transactions
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Getting Started with JSP & Servlet
Using Mapper with Java (Spring)
Getting Started with Spring Boot
Getting Started with Ruby Modules
Getting started with Java and creating an AsciiDoc editor with JavaFX
How to use PlantUML with Visual Studio Code (created on October 30, 2020)
Returning to the beginning, getting started with Java ② Control statements, loop statements
Getting Started with Java_Chapter 5_Practice Exercises 5_4
Code Java from Emacs with Eclim
Try using Redis with Java (jar)
[Java] Boilerplate code elimination using Lombok
Getting started with Swift / C bridges with porting Echo Server using libuv
[Google Cloud] Getting Started with Docker
Java build with mac vs code
Execute packaged Java code with commands
Using Java with AWS Lambda-Eclipse Preparation
[Java] Boilerplate code elimination using Lombok 2
Html5 development with Java using TeaVM
Sample code using Minio from Java
Using proxy service with Java crawling
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 1/3 [Preparation]
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
Spring Boot2 Web application development with Visual Studio Code SQL Server connection
Spring5 MVC web application development with Visual Studio Code SQL Server connection
[Visual Studio Code] I get a syntax error when debugging when using rbenv
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Spring5 MVC Web application development with Visual Studio Code Maven template creation
Getting Started with Machine Learning with Spark "Price Estimate" # 1 Loading Datasets with Apache Spark (Java)
Getting Started with Doma-Criteria API Cheat Sheet
Profiling with Java Visual VM ~ Basic usage ~
Prepare Java development environment with VS Code
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments