Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display all Java compiler warnings in a project using Netbeans? [duplicate]

Tags:

java

netbeans

Coming from Visual Studio and Eclipse, How to find out all the compiler warnings such as unused variables for whole project. It seems like a trivial thing but can't find it. I can find out all errors in "Action Items" window but not all warnings.

like image 569
iraSenthil Avatar asked Nov 04 '13 23:11

iraSenthil


People also ask

How do I show warnings in NetBeans?

Window --> Action Items will show you errors, warnings, and user-generated todo items. Save this answer.

How can I see warnings in eclipse?

For the Target Management Project, we recommend enabling the following compiler warnings in JDT (Window > Preferences > Java > Compiler > Errors/Warnings and Javadoc). Settings that differ from the JDT default are marked in bold.

How do I fix eclipse warnings?

In your Eclipse, go to Window > Preferences > Java > Compiler > Errors/Warnings: Here you could switch any Warning entry to Error. Beware some entries may be hidden by expandable panels, so make sure you expand them all.


2 Answers

It doesn't seem to be possible. There is an open bug report for that: https://netbeans.org/bugzilla/show_bug.cgi?id=135014.

It's old and just recently assigned (see bug history).

As an alternative you can try Source → Inspect. It's not exactly the same thing as the Java compiler warnings, but helps a bit.

For example, in the code below it caught unused imports and the usage of a null variable, but didn't report the unused variable (it was correctly annotated in the NetBeans editor, though).

import java.io.InputStreamReader; // Reported unused import

public class TestJava {
    public static void main(String[] args) {

        String input = null;
        String unused; // DID NOT report unused variable

        System.out.println("Input "+ input); // Reported usage of null
    }
}

Just make sure you install the FindBugs plugin to get the most out of it. The Inspect dialog box will warn that the plugin is missing and install for you.

like image 78
Christian Garbin Avatar answered Sep 20 '22 17:09

Christian Garbin


Honestly, it does not appear to be possible. "Unused Variable" is not even a first-class "Hint" (you can see hints under Tools > Options > Editor > Hints ... and notice that there is no hint based around Unused Variable).

Apparently the only thing about "Unused Variable" you can configure in NetBeans is how it is styled in the Editor. Tools > Options > Fonts and Colors > Syntax > Language=Java > Unused Element.

Even if it were a Hint (it's not), it does not even seem possible to get all actual Hints to display in the "Action Items" pane, even if you create a custom filter that ticks the "Hints-based tasks" box.

It looks like your closest option would be to install the PMD plugin which can list all unused variables across the project (as well as other issues). Tools > Plugins > Available Plugins > tick PMD > Install. Then use Tools > Run PMD. You can configure PMD's rules through Tools > Options > Misc > PMD. PMD does not refresh automatically in my experience (it's on-demand), although there is an "Enable scan" option which seems like it should cause an automatic refresh sometimes [doesn't ever for me].

Other built-in tools for code inspection exist under Source > Inspect. NetBeans has a number of built-in inspectors (including ones based on the aforementioned "Hints"), and a pretty easy-to-enable integration with FindBugs, but none of these inspectors under Source > Inspect to have a rule about Unused Variables. Source > Inspect is also on-demand and does not appear to auto-refresh.

enter image description here

like image 43
Mike Clark Avatar answered Sep 20 '22 17:09

Mike Clark