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
Install the Code Runner extension, and then you have two ways to modify the command
Search
code-runner.executorMap
on the Seting panel, clickEdit in settings.json
Modify in the opening settings.json
for
Right -click
Run Code (Ctrl+Alt+N)
, or the upper right triangle button selectRun Code
Add the following configuration to settings.json
Use the following method to execute the script
Ctrl+Shift+P –>
Run Custom Command (Ctrl+Alt+K)
You can add the following settings to make Code Runner output in the TERMINAL instead of the OUTPUT panel
Under normal circumstances, we still recommend that you use the Extension Pack for Java to obtain more functions and better experiences.
SonarLint may be what you need.