12

I have a legacy webapp which uses jstl and Struts 1 tags. When I pre-compile the JSP files with Java 5/6, the jstl and Struts 1 tags throw warnings about "unchecked or unsafe operations". For example, if I use the following tag:

<%@ page import="/anotherpage.inc" %>

The following warning is thrown:

[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.

If I recompile with -Xlint:unchecked, I get details about the internal working of the offending JSP tag library. I would like to suppress all unchecked operation warnings. I thought that using -Xlint:-unchecked would suppress the warnings, but it did not.

How do I suppress these warnings when compiling my JSP pages? It would not be practical to re-code the JSP tag libraries or update a thousand JSP pages. I'm looking for a compiler flag to globally disable the warning so I can see all warnings except for unchecked warnings. Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bob
  • 217
  • 4
  • 10
  • 1
    Possible duplicate [Is there an equivalent to Java @SuppressWarnings in JSP?](http://stackoverflow.com/questions/487715/is-there-an-equivalent-to-java-suppresswarnings-in-jsp) – justkt Feb 07 '11 at 19:45
  • 1
    In that question, they were asking about something that could be added to an individual JSP to suppress the warning. I'm looking for something global. – Bob Feb 07 '11 at 19:47
  • Warnings are not thrown and they are just warnings. If your code works correctly, you can safely ignore them. – Peter Lawrey Feb 07 '11 at 19:48
  • 1
    Yes, I can ignore the warnings, but I have a few thousand of them and I would like to be able to see legitimate warnings (depreciated APIs, etc...) so that I could correct them. – Bob Feb 07 '11 at 19:49
  • @Bob And what are those warnings? I mean, there is a reason why the Java language evolved on that area... – erloewe Feb 07 '11 at 20:19
  • 1
    Are these warnings generated during your IDE compilation? If you are using Eclipse, you can turn off the JSP validation @ Windows>Preferences>Validation – Nick Feb 07 '11 at 20:53
  • You might want to take a look at this article. http://www.javaworld.com/community/node/5276 – jluzwick Feb 07 '11 at 21:59
  • Technically, that's a "Note" rather than a warning, which makes it quite hard (probably impossible) to turn off. One option would be to use a different compiler. I believe the eclipse compiler will offer more options than standard `javac` – Tim Feb 08 '11 at 03:50
  • This question was also asked on [Sun's Java forum](http://forums.oracle.com/forums/thread.jspa?threadID=1182550&tstart=283) a while back and they also couldn't find an answer. – Bob Feb 08 '11 at 14:08
  • Can't you just post-filter the messages yourself? – OrangeDog Feb 08 '11 at 19:44
  • 6
    The real answer would be: "Java code belongs in Java classes, not in JSP files". But since you've a few thousand of them :/ I feel sorry. – BalusC Feb 08 '11 at 20:19
  • Updating a thousand files would not be that hard with some file processing... Personally, I'd do it with Perl, but that's just me. – pascal Feb 09 '11 at 17:39
  • find the line in javac (you can do just text search), and edit it out. it will take 5mins top. – bestsss Feb 09 '11 at 17:55

3 Answers3

2

Those messages are a (mandatory for JDK >= 1.5) note, not a warning.

compiler.note.unchecked.plural=\
    Some input files use unchecked or unsafe operations.

The default compiler behaviour is the same as with -Xlint:-unchecked.

With -Xlint:unchecked you turn the warning on, reporting each instance.

compiler.warn.unchecked.assign=\
    [unchecked] unchecked assignment: {0} to {1}
...

Mandatory notes cannot be disabled individually, they are all disabled with -Xlint:none. Unfortunately, the rest of the warnings are also disabled.

You can check other responses for alternatives, but filtering compiler output messages seems the easiest solution.

fglez
  • 8,422
  • 4
  • 47
  • 78
  • It appears that mandatory notes will still appear even if `-Xlint:none` is specified. Seems like there is no way around it. I'm having a similar issue, caused by generated code that I don't control (not JSP, but Xtend in my case) :-) – raner Apr 25 '22 at 01:06
1

You are right that

-Xlint:unchecked

does the opposite of what you want, but you can also use

-Xlint:-unchecked

Note the extra "-" in there.

This will disable all warnings about unchecked operations, not just the ones generated by the tag library, but other warnings will still be shown.

Jonathan
  • 349
  • 1
  • 9
  • Actually I already tried that. With Xlint:-unchecked, you get the message "Recompile with Xlint:unchecked" once per offending file. With Xlint:unchecked, you get a detailed message with the exact line number of the tag library violating the unchecked rule. I would like to completely disable the warning and not just shorten the size of the warning. – Bob Feb 08 '11 at 18:51
  • 1
    -1 -Xlint:-unchecked is the default value. That flag keeps displaying the note. – fglez Apr 29 '11 at 11:27
0

the best way to disable that warning is to stop using Java code in your JSPs. Start getting used to using JSTL or JSF instead (with custom tag libs as needed).

But with legacy applications you're not going to be able to do that most likely, and you'll just have to live with the warnings. Of course you can add the -nowarn flag to the compiler, but this will disable ALL warnings, not just this one, which may be more than you want.

jwenting
  • 5,505
  • 2
  • 25
  • 30