如何抑制有关 Sun 专有 API 的 Java 编译器警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1136659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:09:21  来源:igfitidea点击:

How can I suppress java compiler warnings about Sun proprietary API

javaeclipseapibase64sun

提问by tomdee

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 包中的 encode() 方法。如何抑制它生成的编译器警告?

sun.misc.BASE64Encoder is Sun proprietary API and may be removed in

sun.misc.BASE64Encoder 是 Sun 专有的 API,可以在

And as a followup, why don't I see this warning in Eclipse?

作为后续,为什么我在 Eclipse 中看不到这个警告?

采纳答案by JeeBee

You could switch to a different Base64 implementation, e.g., http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.htmlwhich 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.

您可以切换到不同的 Base64 实现,例如http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html,它是 Apache 公共包的一部分,您可能会已经包含在您的类路径或http://iharder.sourceforge.net/current/java/base64/ 中,如果您不希望路径上有另一个 Jar,您可以直接执行并粘贴在您的源代码树中。

回答by Thorbj?rn Ravn Andersen

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.

将调用封装在您放置在类路径上的库 jar 中的类中。然后警告仅在重新编译该库时显示。

回答by wolfy

You can't switch them off, Eclipse simply filters them for you (if told to do so).

您无法关闭它们,Eclipse 只是为您过滤它们(如果被告知这样做)。

Quick interim fix on Linux:

Linux 上的快速临时修复:

javac *.java 2>&1 | pcregrep -v -M ".*Sun proprietary API.*\n.*\n.*\^"

2>&1 ... puts STDERR into STDOUT, so the pipeline "|" will work

2>&1 ... 将 STDERR 放入 STDOUT,因此管道“|” 将工作

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")

pcregrep 可能会或可能不会出现在您的系统上 - 如果没有,请使用您的软件包实用程序(例如在 Debian、Ubuntu 等上:“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).

该表达式搜索“Sun 专有 API”警告和以下两行(包含该行和指示该行中错误位置的“^”)。

I leave the "XY warnings." line in at the end, lest I forget there werewarnings ;o) Note that if you have other warnings as well, the number reported there will of course not be correct :o)

我留下了“XY警告”。在端线,免得我忘了还有警告; O)请注意,如果您有其他的警告,以及,报数会有当然是不正确的:O)

NOTE also that standard "grep" does not work as well, because it can't span multiple lines.

还要注意,标准的“grep”不能很好地工作,因为它不能跨越多行。

回答by Cowan

Eclipse does have a setting which (should) pick this up, see e.g. the desciption in the Eclipse 3.1 release notes:

Eclipse 确实有一个设置(应该)选择它,例如参见Eclipse 3.1 发行说明中的​​说明

Preferences → Java → Compiler → Errors/Warnings → Deprecated And Restricted API → Forbidden/Discouraged Reference

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)

(以及允许项目特定覆盖此设置的项目属性页面)

回答by BertNase

The javacoption -Xlint:uncheckeddoes do the trick: it disables the warnings, see the javac manualfor details

javac选项-Xlint:unchecked确实可以解决问题:它禁用警告,有关详细信息,请参阅javac 手册

回答by Michael Chaves

There is a totally undocumented way to suppress the sun proprietary API warnings! Add -XDignore.symbol.fileto the javaccommand line.

有一种完全没有记录的方法可以抑制 sun 专有 API 警告!添加-XDignore.symbol.filejavac命令行。

I found this at http://bugs.sun.com/bugdatabase/view_bug.do?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!

我在http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6476630(一直滚动到最后一条评论)找到了这个。我们都需要对添加最后一条评论的“xfournet”深思熟虑!

回答by Brandon

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.xmlfile:

如果您了解使用专有 API 的固有问题并决定无论如何都要这样做,并且如果您使用的是 maven,您可能有兴趣将以下内容添加到您的pom.xml文件中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <compilerArgument>-XDignore.symbol.file</compilerArgument>
    </configuration>
</plugin>

回答by Z.I.J

If you are using jdk 1.8 then you can use the below

如果您使用的是 jdk 1.8,那么您可以使用以下

java.util.Base64