user2917747
user2917747

Reputation: 183

How do I treat warnings as errors in specific Java files?

I am working on a Java project that has a lot of compiler warnings. I can't fix all of them at once, but I would like to ensure that each file that becomes warning-free remains warning-free. Is there a way to turn warnings into errors in specific files or packages? I'm interested in generating the errors with either Eclipse or javac.

Upvotes: 4

Views: 2302

Answers (1)

A_Di-Matteo
A_Di-Matteo

Reputation: 27812

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.

enter image description here

At the top right corner you can also select "Configure Project Specific Settings.." if you want to apply this configuration to only a project and not to all projects by default. Alternatively, you can achieve the same by right click on the concerned project > Properties > Java Compiler > Errors/Warnings entry.

Hence, this will be applied to either the whole workspace or just a project, but not to specific files or packages, you don't have this granularity in Eclipse.

This should help you to achieve it, although it might become time and effort consuming to keep it clean, but that's another story.

Moreover, from the Problems view you can create a new Problem View, then from its menu (right top corner of the view, as shown by the picture below), you can configure it. enter image description here

Once created a new Problems View, you can again use the same menu and select the Configure Contents.. entry. From there, you can create a New configuration (see picture below) and define it only for the select element (if you previously selected the concerned class or package), and define whether you want to see any of Errors, Warnings, Info and from which category.

enter image description here

This configuration could help you out to narrow down and filtering errors and warnings on a specific package or class as a complement or as a different solution/approach than the description above.

Final note: I used Eclipse Mars to try it out, but the functionality should be there since many past versions.

Upvotes: 2

Related Questions