{"id":2291,"date":"2022-10-18T18:46:59","date_gmt":"2022-10-18T16:46:59","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2291"},"modified":"2022-10-18T18:47:41","modified_gmt":"2022-10-18T16:47:41","slug":"running-and-debugging-java-in-vs-visual-code","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/10\/18\/running-and-debugging-java-in-vs-visual-code\/","title":{"rendered":"Running and debugging Java in VS (Visual Code)"},"content":{"rendered":"\n<p>Visual Studio Code allows you to debug Java applications through the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=vscjava.vscode-java-debug\" target=\"_blank\">Debugger for Java<\/a>&nbsp;extension. It&#8217;s a lightweight Java debugger based on&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/microsoft\/java-debug\" target=\"_blank\">Java Debug Server<\/a>, which extends the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=redhat.java\" target=\"_blank\">Language Support for Java\u2122 by Red Hat<\/a>.<\/p>\n\n\n\n<p>Here&#8217;s a list of supported debugging features:<\/p>\n\n\n\n<ul><li>Launch\/Attach<\/li><li>Breakpoints<\/li><li>Exceptions<\/li><li>Pause &amp; Continue<\/li><li>Step In\/Out\/Over<\/li><li>Variables<\/li><li>Call Stacks<\/li><li>Threads<\/li><li>Debug Console<\/li><li>Evaluation<\/li><li>Hot Code Replace<\/li><\/ul>\n\n\n\n<h4>Install<\/h4>\n\n\n\n<p>To get the complete Java language support in Visual Studio Code, you can install the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=vscjava.vscode-java-pack\" target=\"_blank\">Extension Pack for Java<\/a>, which includes the Debugger for Java extension.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-1024x136.png\" alt=\"\" class=\"wp-image-2292\" width=\"804\" height=\"106\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-1024x136.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-300x40.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-768x102.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image.png 1347w\" sizes=\"(max-width: 804px) 100vw, 804px\" \/><\/figure><\/div>\n\n\n\n<h4>Configure<\/h4>\n\n\n\n<p>By default, the debugger will run out-of-box by automatically finding the main class and generating a default launch configuration in memory to launch your application.<\/p>\n\n\n\n<p>If you would like to customize and persist your launch configuration, you can select the&nbsp;<strong>create a launch.json file<\/strong>&nbsp;link in the&nbsp;<strong>Run and Debug<\/strong>&nbsp;view.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-1.png\" alt=\"\" class=\"wp-image-2293\" width=\"380\" height=\"268\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-1.png 500w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-1-300x212.png 300w\" sizes=\"(max-width: 380px) 100vw, 380px\" \/><\/figure><\/div>\n\n\n\n<p>The&nbsp;<code>launch.json<\/code>&nbsp;file is located in a&nbsp;<code>.vscode<\/code>&nbsp;folder in your workspace (project root folder).<\/p>\n\n\n\n<p>For more details on how to create the&nbsp;<code>launch.json<\/code>, read&nbsp;<a href=\"https:\/\/code.visualstudio.com\/docs\/editor\/debugging#_launch-configurations\">Launch configurations<\/a>; for more details on configuration options for Java, you can read&nbsp;<a href=\"https:\/\/code.visualstudio.com\/docs\/java\/java-debugging#_configuration-options\">Configuration options<\/a>.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Run and Debug<\/h4>\n\n\n\n<p>The debugger extension provides multiple ways to run and debug your Java application.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Run from CodeLens<\/span><\/h5>\n\n\n\n<p>You will find&nbsp;<strong>Run|Debug<\/strong>&nbsp;on the&nbsp;<a href=\"https:\/\/code.visualstudio.com\/blogs\/2017\/02\/12\/code-lens-roundup\">CodeLens<\/a>&nbsp;of your&nbsp;<code>main()<\/code>&nbsp;function.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-3.png\" alt=\"\" class=\"wp-image-2295\" width=\"538\" height=\"164\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-3.png 695w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-3-300x92.png 300w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/figure><\/div>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Run from Editor menu<\/span><\/h5>\n\n\n\n<p>Another way to start debugging is to select&nbsp;<strong>Run Java<\/strong>&nbsp;or&nbsp;<strong>Debug Java<\/strong>&nbsp;menu from the top editor title bar.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-2.png\" alt=\"\" class=\"wp-image-2294\" width=\"561\" height=\"214\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-2.png 924w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-2-300x115.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/image-2-768x294.png 768w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/figure><\/div>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Run from pressing F5<\/span><\/h5>\n\n\n\n<p>Pressing&nbsp;F5, the debugger will automatically find the entry point of your project and start debugging. You can also start a debugging session from the&nbsp;<strong>Run and Debug<\/strong>&nbsp;view from the side bar of VS Code<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Debugging single files<\/h4>\n\n\n\n<p>In addition to support for debugging Java projects managed by build tools, VS Code also supports for debugging single Java files without any project.<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video autoplay controls loop src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/single-file-debugging-1.mp4\"><\/video><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Debug session inputs<\/h4>\n\n\n\n<p>The default Debug Console in VS Code doesn&#8217;t support inputs. If your program need inputs from a terminal, you can use the Integrated Terminal (Ctrl+`) within VS Code or an external terminal to launch it. You can also use the user setting&nbsp;<code>java.debug.settings.console<\/code>&nbsp;to configure a global console for all Java debug sessions.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/launch-in-terminal.mp4\"><\/video><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Breakpoints<\/h4>\n\n\n\n<p>The Debugger for Java supports various breakpoints, such as line breakpoints, conditional breakpoints, data breakpoints, logpoints.<\/p>\n\n\n\n<h4>Breakpoint &#8211; Conditional breakpoint<\/h4>\n\n\n\n<p>With the help of expression evaluation, the debugger also supports conditional breakpoint. You can set your breakpoint to break when expression evaluates to true.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/conditional-bp.mp4\"><\/video><\/figure>\n\n\n\n<h4>Breakpoint &#8211; Data breakpoint<\/h4>\n\n\n\n<p>You can have the debugger break when a variable changes its value. Note that the data breakpoint can only be set inside a debug session. This means you need to launch your application and break on a regular breakpoint first. You can then pick a field in the&nbsp;<strong>VARIABLES<\/strong>&nbsp;view and set a data breakpoint.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/data-breakpoint-1024x768.png\" alt=\"\" class=\"wp-image-2301\" width=\"591\" height=\"443\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/data-breakpoint-1024x768.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/data-breakpoint-300x225.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/data-breakpoint-768x576.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/data-breakpoint.png 1028w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/figure><\/div>\n\n\n\n<h4>Breakpoint &#8211; Logpoints<\/h4>\n\n\n\n<p><a href=\"https:\/\/code.visualstudio.com\/blogs\/2018\/07\/12\/introducing-logpoints-and-auto-attach#_introducing-logpoints\">Logpoints<\/a>&nbsp;is also supported by Java Debugger. Logpoints allow you to send output to Debug Console without editing code. They&#8217;re different from breakpoints because they don&#8217;t stop the execution flow of your application.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/logpoints.mp4\"><\/video><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Expression evaluation<\/h4>\n\n\n\n<p>The debugger also lets you evaluate expressions in the&nbsp;<strong>WATCH<\/strong>&nbsp;window as well as the Debug Console.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/expression-evaluation.mp4\"><\/video><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Hot Code Replace<\/h4>\n\n\n\n<p>Another advanced feature the debugger supports is &#8216;Hot Code&#8217; replacement. Hot Code Replace (HCR) is a debugging technique whereby the Debugger for Java transmits the class changes over the debugging channel to another Java Virtual Machine (JVM). HCR facilitates experimental development and fosters iterative trial-and-error coding. With this new feature, you can start a debugging session and change a Java file in your development environment, and the debugger will replace the code in the running JVM. No restart is required, which is why it&#8217;s called &#8220;hot&#8221;. Below is an illustration of how you can use HCR with Debugger for Java in VS Code.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/hcr.mp4\"><\/video><\/figure>\n\n\n\n<p>You may use the debug setting&nbsp;<code>java.debug.settings.hotCodeReplace<\/code>&nbsp;to control how to trigger Hot Code Replace. The possible setting values are:<\/p>\n\n\n\n<ul><li><code>manual<\/code>&nbsp;&#8211; Click the toolbar to apply the changes (default).<\/li><li><code>auto<\/code>&nbsp;&#8211; Automatically apply the changes after compilation.<\/li><li><code>never<\/code>&nbsp;&#8211; Disable Hot Code Replace.<\/li><\/ul>\n\n\n\n<h4 id=\"_step-filtering\">Step filtering<\/h4>\n\n\n\n<p>Step filter is supported by the extension to filter out types that you do not want to see or step through while debugging. With this feature, you can configure the packages to filter within your&nbsp;<code>launch.json<\/code>&nbsp;so they could be skipped when you step through.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/step-filter.mp4\"><\/video><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 id=\"_configuration-options\">Configuration options<\/h4>\n\n\n\n<p>There are many options and settings available to configure the debugger. For example, configuring the JVM arguments and environment variables is easily done with launch options.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/10\/launch-configuration.mp4\"><\/video><\/figure>\n\n\n\n<p>Consult the documentation for the&nbsp;<a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=redhat.java\" target=\"_blank\" rel=\"noreferrer noopener\">Language Support for Java\u2122 by Red Hat<\/a>&nbsp;extension for help with setting up your project.<\/p>\n\n\n\n<p>For many commonly used setups, there are samples available in&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/microsoft\/vscode-java-debug\/blob\/main\/Configuration.md\" target=\"_blank\">VS Code Java Debugger Configuration<\/a>. The document explains how the Java debugger automatically generates configurations for you, and if you need to modify them, how to do so with Main class, different arguments, environment, attaching to other Java processes, and usage of more advanced features.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Reference:  <em>https:\/\/code.visualstudio.com\/<\/em><\/h5>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio Code allows you to debug Java applications through the&nbsp;Debugger for Java&nbsp;extension. It&#8217;s a lightweight Java debugger based on&nbsp;Java Debug Server, which extends the&nbsp;Language Support for Java\u2122 by Red Hat. Here&#8217;s a list of supported debugging features: Launch\/Attach Breakpoints Exceptions Pause &amp; Continue Step In\/Out\/Over Variables Call Stacks Threads Debug Console Evaluation Hot Code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[620,554],"tags":[574,161,217],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2291"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2291"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2291\/revisions"}],"predecessor-version":[{"id":2309,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2291\/revisions\/2309"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/2307"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}