JAVA 基礎--開發環境 vscode 搭建

來源:互聯網
上載者:User

標籤:article   eve   support   which   track   store   efi   ant   current   

這裡先放一篇文章和一篇官方文檔

 

對於使用 Visual Studio Code 的 Java 開發人員來說,Language Support for Java(TM) by Red Hat 擴充提供了非常好的語言特性支援,比如智能感知和項目支援等。然而,我們也聽到一些使用者反饋需要調試 Java 程式。今天,我們萬分激動地宣布,我們與 Red Hat 合作開發了能讓開發人員調試 Java 應用程式的輕量級擴充:Java Debugger for Visual Studio Code,它基於 Java Debug Server。

注意: Java Debugger for Visual Studio Code 和 Java Debug Server 都將在發布初始版後不久開放源碼。

 

為了讓 Java 開發人員能儘快開始使用 VS Code,我們也建立了一個 Java 擴充包,它包含了 Language Support for Java(TM) by Red Hat 和 Java Debugger for Visual Studio Code 這兩個擴充,這樣你不需要單獨去搜尋安裝這些擴充。這是剛剛開始建立的一個現代 Java 工作流程,將來我們會往 Java 擴充包中添加更多特性和擴充。

 開始

從以下步驟開始:

  1. 在 Visual Studio Code 中開啟擴充視圖(Ctrl+Shift+X)。

  2. 輸入“java”以過濾擴充列表。

  3. 找到並安裝 Java Extension Pack (Java 擴充包),如果你已經安裝了 Language Support for Java(TM) by Red Hat,也可以單獨找到並安裝 Java Debugger for Visual Studio Code 擴充。

你也可以直接從本文中安裝 Java 擴充包:

安裝 Java 擴充包    (安裝了 VSCode 的情況下,點擊上面的連結即可調用 VSCode 進行安裝)

重啟 VSCode,開啟包含 Java 項目的代碼,然後按下面的步驟進行:

  1. 準備項目。開啟一個 .java 檔案,Java 擴充會啟用。Maven、Gradle 和 Eclipse 項目都能得到支援。這個擴充會自動構建項目,不需要手工觸發構建。

  2. 開始調試。切換到調試視圖(Ctrl+Shift+D),開啟 launch.json 並添加 Java 調試配置。

  3. 在 mainClass 中填入要啟動的類,或者在 hostName 中填入要附加的主機名稱以及在 port 中填入連接埠。

  4. 設定斷點並按 F5 開始調試。

支援的特性

在這個版本中,我們支援以下特性:

  • 啟動/附加 - 你可以在 VS Code 中啟動一個 Java 項目,也可以附加到一個在本地或遠程端以偵錯模式啟動並執行 JVM 進程。

  • 斷點 - 支援運算式條件或命中條件斷點,可以在內建的斷點設定容器中輕鬆進行設定。因此你可以直接在原始碼視圖中插入斷點,不需要彈出容器,非常方便。按異常中斷也得到了支援。

  • 控制流程 - 包括 暫停、繼續 F5、單步 F0、步進 F11、步出(跳出) Shift+F11。

  • 資料審查 - 當程式停止在斷點上的時候,調試器可以訪問當前記憶體中的變數名和值。支援查看/監視/設定變數值。

  • 診斷 - 調用棧 面板會顯示程式的調用棧,你在這個面板中可以在捕獲的調用路徑中隨意跳轉。多線程調試也通過並行棧得到支援。

  • 偵錯主控台 - 可以從偵錯主控台看到標準輸出和錯誤輸出資訊。

官方文檔: 

The Java support in VS Code is provided through extensions and optimized for lightweight Java projects with a simple, modern workflow. Popular extensions in the Marketplace provide project support, code completion, linting, debugging, code formatting, snippets, and more.

Download VS Code - If you haven‘t downloaded VS Code yet, quickly install for your platform (Windows, Mac, Linux).

Install Java Extensions

VS Code is a fast editor and ships with only the basic features. Add Java language support to VS Code by installing the popular Java extensions.

  1. In Visual Studio Code, go to the Extensions view (Ctrl+Shift+X).
  2. Filter the extensions list by typing "java".

To help set up Java on VS Code, there is a Java Extension Pack which contains two popular extensions:

  1. Language Support for Java(TM) by Red Hat
  2. Java Debugger for Visual Studio Code

Install the Java Extension Pack

This document describes the features included in the Java Extension Pack.

Java Project Support

Maven, Eclipse and Gradle Java project are supported through Language Support for Java(TM) by Red Hat, by utilizing M2Eclipse which provides Maven support and Buildship which provides Gradle support through the Eclipse JDT Language Server.

Please note that Gradle-based Android projects are not supported.

Editing and Navigating Code

Linting

A linter is a tool that provides warnings for suspicious looking code. Language Support for Java(TM) by Red Hat provides this feature to report parsing and compilation errors as you type, so you can fix them inside VS Code.

Intellisense

VS Code also supports code completion and Intellisense for Java through Language Support for Java(TM) by Red Hat. Intellisense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and thirty-party modules.

Search for symbols

You can search for symbols in the current file or workspace to navigate your code more quickly.

To search for a symbol in the current file, use Quick Open (Ctrl+P) then enter the ‘@‘ command, then enter the name of the symbol you‘re looking for. A list of potential matches will appear and be filtered as you type. Choose from the list of matches to navigate to its location.

To search for a symbol in the current workspace, start by pressing Ctrl+T, then enter the name of the symbol. A list of potential matches will appear as before. If you choose a match that was found in a file that‘s not already open, the file will be opened before navigating to the match‘s location. Alternatively, you can also use Quick Open (Ctrl+P) then enter the ‘#‘ command to search the current workspace.Ctrl+T is just the shortcut for the ‘#‘ commands, respectively, so everything works the same.

Peek Definition

You can take a quick look at how a symbol was defined by using the Peek Definition feature. This feature displays a few lines of code near the definition inside a peek window so you can take a look without navigating away from your current location.

To peek at a symbol‘s definition, place your cursor on the symbol anywhere it‘s used in your source code and then press Alt+F12. Alternatively, you can choose Peek Definition from the context menu (right-click, then choose Peek Definition).

Go to Definition

You can also quickly navigate to where a symbol is defined by using the Go to Definition feature.

To go to a symbol‘s definition, place your cursor on the symbol anywhere its used in your code and then press F12. Alternatively, you can choose Go to Definition from the context menu (right-click, then chooseGo to Definition). When there‘s only one definition of the symbol, you‘ll navigate directly to its location, otherwise the competing definitions are displayed in a peek window as described in the previous section and you have to choose the definition that you want to go to.

Other code editing features include:

  • Code formatting
  • Code snippets
  • Code outline
  • Code lens (references)
  • Javadoc hovers
  • Rename
  • Highlights
Debugging

Java Debugger for Visual Studio Code is a lightweight Java Debugger based on Java Debug Server. It works with Language Support for Java by Red Hat to allow users debugging Java code using Visual Studio Code (VS Code).

The Java Debugger supports following features:

  • Launch/Attach - You can either launch the Java project within VS Code or attach to any running JVM process in debug mode, locally or remotely.
  • Breakpoints - Conditional breakpoints by Hit Count is supported and can easily be set using the inline breakpoint settings window. This allows you to conveniently add conditional breakpoints to your code, directly in the source viewer, without requiring a modal window. Break on exceptions is also supported.
  • Control flow - Including Pause, Continue F5, Step over F10, Step into F11, Step out Shift+F11
  • Data inspection - When you‘re stopped at a breakpoint, the debugger has access to the variable names and values that are currently stored in memory. Inspect/Watch/Set Variables are supported.
  • Diagnostics - The CALL STACK panel shows the call stack of your program and allows you to navigate through the call path of each captured allocation. Multi-threaded debugging is supported by parallel stacks.
  • Debug Console - The Debug Console lets you see information from both stdout and stderr.

You can launch the debugger following steps below:

  1. Switch to the Debug view (Ctrl+Shift+D).
  2. Open launch.json to add a debug configuration for Java
  3. Fill in the mainClass for Launch setting or hostName and port for Attach.
  4. Click Start button or press F5 to start debugging.

Next Steps

Read on to find out about:

  • Basic Editing - Learn about the powerful VS Code editor.
  • Code Navigation - Move quickly through your source code.
  • Tasks - use tasks to build your project and more
  • Debugging - find out how to use the debugger with your project
轉載自: http://blog.csdn.net/bat67

JAVA 基礎--開發環境 vscode 搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.