I'm using a parser generator that creates somewhat ugly code. As a result my Eclipse project has several dozen warnings emanating from generated source files. I know I can use the @SuppressWarning
annotation to suppress particular warnings in particular elements, but any annotations I add by hand will be lost when the parser generator runs again. Is there a way to configure Eclipse to suppress warnings for a particular file or directory?

- 88,451
- 51
- 221
- 321

- 55,321
- 43
- 129
- 155
-
Related to http://stackoverflow.com/questions/230199/filtering-warnings-in-eclipse-by-filename – Gray Feb 29 '12 at 18:10
13 Answers
Starting with version 3.8 M6, Eclipse (to be exact: the JDT) has built-in functionality for this. It is configurable through a project's build path: Project properties > Java Build Path > Compiler > Source
Announced here: Eclipse 3.8 and 4.2 M6 - New and Noteworthy, called Selectively ignore errors/warnings from source folders. That's also where the screenshot is from. This is the new feature developed on the previously linked Bug 220928.

- 9,924
- 6
- 56
- 69
-
3I couldn't find how to switch "No" -> "Yes" ... it's "Toggle" button (and not edit or something else) ;-) – Betlista Oct 02 '12 at 11:09
-
@Betlista: Just double-click it. Or click Toggle button on the right. – altumano Nov 02 '12 at 10:53
-
1@hheimbuerger: this solution works for source folders only. But what if I just have a folder in project containing some malformed XML files? I hate to see warnings about them :( – altumano Nov 02 '12 at 10:55
-
@altumano The above feature/option comes from the JDT plugin, i.e. the Java support for Eclipse. (Remember that pretty much everything in Eclipse is a plugin, even the Java support is not baked in.) So you'd have to check back with the developer of the plugin you use to do the XML validation. IIRC, there are numerous, so you should probably open a new question specifically for the one you are using. – Henrik Heimbuerger Nov 04 '12 at 21:54
-
+1. By the way, which OS did you use? Font rendering is quite strange. – Display Name Nov 17 '13 at 20:19
-
@SargeBorsch It is! But I think I took the screenshot from the Eclipse release notes and then annotated it, so I don't know what OS they use. Certainly doesn't look like that on my machine. – Henrik Heimbuerger Nov 17 '13 at 20:54
-
@HDave FYI, the URL you posted didn't actually contain the bug ID. – Henrik Heimbuerger Aug 25 '15 at 07:47
-
This solution fails if you make use of Maven M2E. Here's a link to the 3 year old bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=388541 – Dave Aug 26 '15 at 16:11
There is a ticket for this, Bug 220928, that has since been completed for Eclipse 3.8. Please see this answer for details.
If you're stuck with Eclipse 3.7 or lower: The user "Marc" commenting on that ticket created (or at least links to) a plugin called 'warningcleaner' in comment 35. I'm using that with a lot of success while waiting for this feature to be integrated into Eclipse.
It's really quite simple:
- Install plugin.
- Right-click project and select "Add/remove generated code nature".
- Open the project settings (right-click and select "properties").
- Open the tab 'Warning Cleaner'.
- Select the source folders you want to ignore the warnings from.

- 1
- 1

- 9,924
- 6
- 56
- 69
-
-
Damn, that sucks. I still have it, but I'd rather not upload it without explicit permission from the author, in particular as it specifies no license at all. Can you try to track down the original author and ask him to reupload it please? You could post on the bug for example, he is still listed as CC on it. – Henrik Heimbuerger May 20 '11 at 15:51
-
1It appears that progress is still being made (reading the comments on the bug) towards a core implementation. Make sure to update the question/answer to use specific version numbers if the feature is implemented. Otherwise, future users of Eclipse may be confused. – Chris Browne Oct 25 '11 at 04:04
-
@Chris Browne You're welcome to edit question and answer if you think it can be improved. :) – Henrik Heimbuerger Oct 25 '11 at 07:41
-
1I don't feel the question/answer can currently be improved, since the functionality has yet to exist, but if the functionality is ever added to eclipse then someone should edit this. Not necessarily me, but if I'm in the right place at the right time then of course I will do so. – Chris Browne Oct 26 '11 at 01:54
-
5Does anyone have a current link to this plugin? I'd love to be able to use it! – Tom Tresansky Jan 18 '12 at 13:55
-
1Attention: Eclipse now has a built-in solution for this, as you can see in the other answer from hheimbuerger. By the way, hheimbuerger, you should refactor this answer with updated info to make it the 'official'. – Roberto Apr 25 '12 at 17:55
-
@Roberto I edited a link in, but mostly, you should just upvote the other answer so it appears over this one. :) – Henrik Heimbuerger Apr 26 '12 at 00:47
-
https://github.com/mflament/eclipse-warning-cleaner/releases is the new location of this plugin. – Jason C Jun 23 '17 at 00:46
-
> "... is the new location of this plugin": not any more: 404 not found... :-( – mmo Sep 12 '22 at 10:10
-
@mmo It's been more than 11 years now... I feel for you if you're still stuck on Eclipse 3.7. (Nevertheless, I hate it when GitHub projects are deleted!) – Henrik Heimbuerger Sep 16 '22 at 09:35
I solved this by using the maven regexp replace plugin - it does not solve the cause, but heals the pain:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/generated-sources/antlr/**/*.java</include>
</includes>
<regex>true</regex>
<regexFlags>
<regexFlag>MULTILINE</regexFlag>
</regexFlags>
<replacements>
<replacement>
<token>^public class</token>
<value>@SuppressWarnings("all") public class</value>
</replacement>
</replacements>
</configuration>
</plugin>
Note that I did not manage to get the ** notation to work, so you might have to specify path exactly.
See comment below for an improvement on how not to generate duplicate @SupressWarnings
-
As Maven was not mentioned, this does not necessarily answer the question. But it works great in my case, as I use Maven ;-) – Kutzi Dec 08 '10 at 08:36
-
The same thing can be done if you're using Ant instead of Maven, see my answer. – Jorn Feb 23 '11 at 22:59
-
1it seems that ANTLR 3.4 adds the annotation by itself, but I like the generality of the solution (not only ANTLR generates code). To still apply it to all generated sources, I use this pattern: `^(@SuppressWarnings\(.*?\)\s+)?public class`. By including the annotation in the pattern, it isn't duplicated if it's already there. – Silly Freak Dec 24 '11 at 13:15
-
This (more or less) worked for me -- I had to add `${basedir}/` before `target` in the `
` tag. Feels kinda janky, but since it only works on generated files, I'll take it! – Rob Jan 19 '17 at 19:47
I think the best you can do is enable project specific settings for displaying warnings.
Window -> Preferences -> Java -> Compiler -> Errors/Warnings
On the top of the form is a link for configuring project specific settings.

- 136,852
- 53
- 295
- 323
-
That only allows to adjust errors/warnings/infos on a *project* level but not to exclude *specific directories*. – mmo Sep 14 '22 at 20:30
User @Jorn hinted at Ant code to do this. Here's what I have
<echo>Adding @SuppressWarnings("all") to ANTLR generated parser/lexer *.java</echo>
<echo> in ${project.build.directory}/generated-sources/antlr/</echo>
<replace dir="${project.build.directory}/generated-sources/antlr/"
summary="true"
includes="**/*.java"
token="public class"
value='@SuppressWarnings("all") public class' />
Note that Ant's <replace> does text replacement, not regular expression replacement, so it cannot use the ^ meta-character in the token to match beginning of line as the maven regexp replace plugin does.
I'm doing this at the same time that I run Antlr from maven-antrun-plugin in my Maven pom, because the ANTLR maven plugin did not play well with the Cobertura maven plugin.
(I realize this is not an answer to the original question, but I can't format Ant code in a comment/reply to another answer, only in an answer)

- 4,930
- 1
- 34
- 37
I don't think Eclipse inherently provides a way to do this at the directory level (but I'm not sure).
You could have the generated files go into a separate Java project, and control warnings for that specific project.
I generally prefer to place automatically-generated code in a separate project anyway.

- 88,451
- 51
- 221
- 321
You can only suppress warnings at the project level. However, you can configure your problems tab to suppress warnings from files or packages. Go into the Configure Contents menu and work with the "On working set:" scope.

- 1,126
- 2
- 11
- 26
-
-
You can't find it or you can't figure out where to navigate in it? I'm using Eclipse 3.4.1 (I think it a Ganymede install with PyDev added). It's located on the upper right hand corner of the Problems tab when you click on the little arrow icon to drop down the menu for that tab. – Greg Jul 14 '09 at 21:03
-
I can find it. I don't understand what changing the settings in that dialog would accomplish. – Chris Conway Jul 14 '09 at 21:27
-
I can see how this could work, but then you'd have to use working sets, which isn't a generic solution. It adds to the problem because now you have to update your working set to see the right warnings. – Peter Dolberg Mar 08 '12 at 20:14
This small python script "patches" the M2E-generated .classpath
files and adds the required XML tag to all source folders starting with target/generated-sources
. You can just run it from you project's root folder. Obviously you need to re-run it when the Eclipse project information is re-generated from M2E. And all at your own risk, obviously ;-)
#!/usr/bin/env python
from xml.dom.minidom import parse
import glob
import os
print('Reading .classpath files...')
for root, dirs, files in os.walk('.'):
for name in files:
if (name == '.classpath'):
classpathFile = os.path.join(root, name)
print('Patching file:' + classpathFile)
classpathDOM = parse(classpathFile)
classPathEntries = classpathDOM.getElementsByTagName('classpathentry')
for classPathEntry in classPathEntries:
if classPathEntry.attributes["path"].value.startswith('target/generated-sources'):
# ensure that the <attributes> tag exists
attributesNode = None;
for attributes in classPathEntry.childNodes:
if (attributes.nodeName == 'attributes'):
attributesNode = attributes
if (attributesNode == None):
attributesNode = classpathDOM.createElement('attributes')
classPathEntry.appendChild(attributesNode)
# search if the 'ignore_optional_problems' entry exists
hasBeenSet = 0
for node in attributesNode.childNodes:
if (node.nodeName == 'attribute' and node.getAttribute('name') == 'ignore_optional_problems'):
# it exists, make sure its value is true
node.setAttribute('value','true')
#print(node.getAttribute('name'))
hasBeenSet = 1
if (not(hasBeenSet)):
# it does not exist, add it
x = classpathDOM.createElement("attribute")
x.setAttribute('name','ignore_optional_problems')
x.setAttribute('value','true')
attributesNode.appendChild(x)
try:
f = open(classpathFile, "w")
classpathDOM.writexml(f)
print('Writing file:' + classpathFile)
finally:
f.close()
print('Done.')

- 11
- 1
I'm doing this to a few ANTLR grammars, which generate a Java parser using Ant. The Ant build script adds the @SuppressWarnings("all")
to one Java file, and @Override
to a few methods in another.
I can look up how it's done exactly, if you're interested.

- 20,612
- 18
- 79
- 126
-
An interesting idea. Doesn't the @SuppressWarnings need to come just before the class declaration (i.e., it's not as easy as inserting it on the first line of the file)? – Chris Conway Jul 14 '09 at 22:00
-
It does need to be placed there, but it's doable. I needed to dive deep into the ant documentation to find the right function tough, but I don't have a lot of experience with Ant. – Jorn Jul 14 '09 at 22:08
-
See http://www.antlr.org/wiki/pages/viewpage.action?pageId=1865 which describes how to modify the ANTLR Java codegen template. However, it is not clear how to make that work when using the ANTLR plugin for Eclipse. I guess one is left with hacking the plugin jar or using one of the other workarounds listed above. – djb Feb 23 '11 at 18:38
It's been a while since I have released the warning-cleaner plugin, and now that I am using Eclipse 3.8, I have no need for it anymore. However, for those who still need this plugin, I have released it on github with the update site on bintray. If you are still using Eclipse 3.7 or before, this could be useful. Check this site for installation details.

- 116
- 2
- 3
In the case of ANTLR 2, it is possible to suppress warnings in generated code by appenidng @SuppressWarnings
before the class declaration in the grammar file, e.g.
{@SuppressWarnings("all")} class MyBaseParser extends Parser;

- 3,135
- 2
- 29
- 36
This can be done by excluding certain directories from the build path (The following example is given using Eclipse 3.5)
[1] Bring up the Java Build Path
- Click on the projectin Package Explorer
- Right click, properties
- Select Java Build Path
[2] Add directories to exclude
- The Source tab should contain details of the project source folders
- Expand the source folder and locate the 'Excluded:' property
- Select 'Excluded:' and click Edit
- Add folders into the Exclusion patterns using the Add/Add Multiple options
- Click Finish, then ok for Eclipse to rebuild.

- 1,159
- 1
- 7
- 3
If the eclipse project is generated from gradle using Eclipse plugin's eclipse
command the Selectively ignore errors/warnings from source folders
option can be set by adding this on the top level of you build.gradle
file:
eclipse.classpath.file {
whenMerged { classpath ->
classpath.entries.each { entry ->
if (entry.path.contains('build/generated/parser')) {
entry.entryAttributes['ignore_optional_problems'] = true
}
}
}
}
This assumes that generated sources are in build/generated/parser
folder.

- 14,905
- 3
- 48
- 53