如何抑制有关 Sun 专有 API 的 java 编译器警告
我正在使用 sun.misc.BASE64Encoder 包中的encode() 方法。 如何抑制它生成的编译器警告?
sun.misc.BASE64Encoder 是 Sun 专有 API,可能会在
中删除
并且作为后续操作,为什么我在 Eclipse 中看不到此警告?
I'm using the encode() method from the sun.misc.BASE64Encoder package. How do I suppress the compiler warnings that it generates?
sun.misc.BASE64Encoder is Sun proprietary API and may be removed in
And as a followup, why don't I see this warning in Eclipse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
有一种完全没有记录的方法可以抑制 sun 专有 API 警告!
将
-XDignore.symbol.file
添加到javac
命令行。我在 https://bugs.java.com/bugdatabase/view_bug?bug_id= 找到了这个6476630(一直滚动到最后一条评论)。 我们都需要对添加最后一条评论的“xfournet”有善意的想法!
There is a totally undocumented way to suppress the sun proprietary API warnings!
Add
-XDignore.symbol.file
to thejavac
command line.I found this at https://bugs.java.com/bugdatabase/view_bug?bug_id=6476630 (scroll all the way to the very last comment). We all need to think kind thoughts about "xfournet" who added that last comment!
您可以切换到不同的 Base64 实现,例如 http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html 这是 Apache commons 包的一部分,您可能已经将其包含在类路径中,或 http://iharder.sourceforge.net/current/java/base64/ 其中如果您不希望路径上有另一个 Jar,您可以直接采用实现并粘贴在源代码树中。
You could switch to a different Base64 implementation, e.g., http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html which is part of the Apache commons packages, which you might already have included in your classpath, or http://iharder.sourceforge.net/current/java/base64/ which you could just take the implementation and stick in your source tree if you don't want another Jar on the path.
因为您无法禁用此警告
because you cannot disable this warning
Eclipse 确实有一个设置(应该)选择它,请参见 Eclipse 3.1 发行说明:
首选项 → Java → 编译器 → 错误/警告 → 已弃用和受限制的 API → 禁止/不鼓励的参考
(以及允许项目特定的项目属性页面覆盖此设置)
Eclipse does have a setting which (should) pick this up, see e.g. the desciption in the Eclipse 3.1 release notes:
Preferences → Java → Compiler → Errors/Warnings → Deprecated And Restricted API → Forbidden/Discouraged Reference
(and the project properties page to allow project-specific overrides of this setting)
您无法关闭它们,Eclipse 只是为您过滤它们(如果被告知这样做)。
Linux 上的快速临时修复:
2>&1 ... 将 STDERR 放入 STDOUT,因此管道“|” 可以工作
pcregrep 可能存在也可能不存在于您的系统上 - 如果没有,请使用您的软件包实用程序(例如在 Debian、Ubuntu 等上:“sudo apt-get install pcregrep”)
该表达式搜索“Sunproprietary API”警告和下面两行(包含该行和指示该行中错误位置的“^”)。
我留下“XY 警告”。 在最后排队,以免我忘记有警告;o)
请注意,如果您还有其他警告,则报告的数字当然不正确:o)
另请注意,标准“grep”也不起作用,因为它不能跨越多行。
You can't switch them off, Eclipse simply filters them for you (if told to do so).
Quick interim fix on Linux:
2>&1 ... puts STDERR into STDOUT, so the pipeline "|" will work
pcregrep might or might not be present on your system - if not, use your package utility (e.g. on Debian, Ubuntu etc: "sudo apt-get install pcregrep")
The expression searches for the "Sun proprietary API" warning and the following two lines (containing the line and the "^" indicating the position of the error in the line).
I leave the "XY warnings." line in at the end, lest I forget there were warnings ;o)
Note that if you have other warnings as well, the number reported there will of course not be correct :o)
NOTE also that standard "grep" does not work as well, because it can't span multiple lines.
如果您了解使用专有 API 的固有问题并决定无论如何都要这样做,并且您使用的是 Maven,那么您可能有兴趣将以下内容添加到您的
pom.xml
文件中:If you understand the inherent problems with using a proprietary API and decide to do it anyway, and if you are using maven, you might be interested in adding the following to your
pom.xml
file:如果你使用的是jdk 1.8那么你可以使用下面的
If you are using jdk 1.8 then you can use the below
将调用封装在类中,并将其放置在类路径上的库 jar 中。 然后,仅在重新编译该库时才会显示警告。
Encapsulate the call in a class which you place in a library jar on your classpath. Then the warning only shows when that library is recompiled.
javac
选项-Xlint:unchecked
确实可以解决问题:它禁用警告,请参阅 javac 手册 了解详细信息The
javac
option-Xlint:unchecked
does do the trick: it disables the warnings, see the javac manual for details