skip to Main Content

The Java extensions for VSCode do an excellent job of catching most warnings, but recently I came across several warnings in my project that VSCode was not reporting but which I found through compiling using the -Xlint flag.

Take this SSCCE for example:

public class Test
{
    public static void main(String[] args)
    {
        int x = 3;
        int y = (int)x;

        System.out.println(y);
    }
}

This code runs perfectly fine with no warnings in VSCode, but if you compile it with javac -Xlint Test.java, you will get this:

Test.java:6: warning: [cast] redundant cast to int
        int y = (int)x;
                ^      
1 warning

What I would like is a way of having these warnings get reported in VSCode just like other warnings (where they are highlighted with yellow squiggles that you can mouse over). I have tried, unsuccessfully, to see if there was a way of changing the default javac compiler arguments but have not found the configuration option (if it exists).

And the above warning is just a single example of things VSCode does not appear to report. I have found other warnings through using the -Xlint flag as well, such as fields that should be marked transient that are not.

2

Answers


  1. Install the Code Runner extension, and then you have two ways to modify the command

    1. Search code-runner.executorMap on the Seting panel, click Edit in settings.json

      enter image description here

    2. Modify in the opening settings.json

              "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
      

      for

              "java": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
      

      enter image description here

    3. Right -click Run Code (Ctrl+Alt+N), or the upper right triangle button select Run Code

      enter image description here


    1. Add the following configuration to settings.json

          "code-runner.customCommand": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
      
    2. Use the following method to execute the script

      Ctrl+Shift+P –> Run Custom Command (Ctrl+Alt+K)

      enter image description here


    You can add the following settings to make Code Runner output in the TERMINAL instead of the OUTPUT panel

        "code-runner.runInTerminal": true,
    

    Under normal circumstances, we still recommend that you use the Extension Pack for Java to obtain more functions and better experiences.

    Login or Signup to reply.
  2. SonarLint may be what you need.

    SonarLint is an easy-to-use extension that helps you find and fix bugs and security issues as you code. The extension runs in the background and, just like a spell checker, highlights source code issues that pose a quality or security concern. The extension not only tells you what the issue is but also provides in-context guidance on why it’s harmful and how to fix it, with examples. The extension supports over 500+ Java rules and includes several Quick Fixes to automatically fix certain quality issues

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search