1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Visual Studio CodeでのJava開発環境の基本

Last updated at Posted at 2021-08-15

やりたいこと

  • JavaのVersion管理
  • チェックスタイル
  • TestをIDE上で実行
  • DebugをIDE上で実行
  • 環境変数の設定
  • BreakpointでのDebug

Java Runtime

java.home in settings.json

Command + Shift + P

image.png

image.png

Checkstyle

check_style.xml があれば、右クリックして set the Checkstyle Configuration fileをクリックする

スクリーンショット 2021-08-04 11.53.53.png

settings.json
"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",

一点不明なのが、なぜかVSCodeだと以下のようにフォーマットされるが、

  public int distinctValues(String fldname) {
    if (p1.schema().hasField(fldname))
      return p1.distinctValues(fldname);
    else
      return p2.distinctValues(fldname);
  }

Google Java Formatだと以下のようになってしまう (CIとずれてしまい困る..多分VSCode側が変?)

  public int distinctValues(String fldname) {
    if (p1.schema().hasField(fldname)) return p1.distinctValues(fldname);
    else return p2.distinctValues(fldname);
  }

Test

Test Runner for Java というExtentionをインストールして上のRuntimeがちゃんと設定できていれば、VSCode上でテストが実行できる

Debugする

「実行とデバッグ」のアイコンをクリックすると以下のように出るので、

Screen Shot 2023-02-11 at 16.07.09.png

「launch.jsonファイルを作成します」をクリックする。
環境変数envや引数argsなどを設定する。詳細はjava debugging

launch.json
{
    // 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": "Launch Current File",
            "request": "launch",
            "mainClass": "${file}"
        },
        {
            "type": "java",
            "name": "Launch SampleApplication",
            "request": "launch",
            "mainClass": "com.nakamasato.testapp.SampleApplication",
            "projectName": "testapp",
            "env":{
                "TEST_ENV": "value"
            }
        }
    ]
}

BreakpointでのDebug

  1. Breakpointをつける

    image.png

  2. DebugでRunする

  3. 確認

    image.png

感想

  • 一通り設定はできた
  • intellijの方がJava開発にはいいと聞くのでいつか比較も

参考

  1. https://medium.com/@tariqul.islam.rony/learning-java-and-spring-boot-with-visual-studio-code-vscode-part-1-54073f2fa264
  2. https://code.visualstudio.com/docs/editor/debugging
  3. https://github.com/diffplug/spotless/tree/main/plugin-gradle
  4. https://tech.excite.co.jp/entry/2022/09/05/162416
  5. https://medium.com/@anirudhramesh95/enforcing-formatting-standards-for-your-java-project-using-gradle-2c21172743e5
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?