16

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
tomdee
  • 2,319
  • 5
  • 25
  • 39
  • 1
    Could replace it with another implementation, e.g., http://iharder.sourceforge.net/current/java/base64/ – JeeBee Jul 16 '09 at 10:21
  • 5
    Or http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html – JeeBee Jul 16 '09 at 10:21
  • Rather than suppress the warning, why not [use the Java 8 supplied encoder rather than the proprietary API](https://stackoverflow.com/questions/13109588/base64-encoding-in-java)? – Raedwald Jun 04 '19 at 11:12

9 Answers9

25

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

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!

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Michael Chaves
  • 251
  • 3
  • 2
  • 4
    It's worth remembering why the warning exists, and why it's not supposed to be possible to shut it up, not with `@SuppressWarnings("all")`, or `-nowarn` or `-Xlint:none` or `-Xmaxwarns 0`. If you depend on internal classes your program will break sooner or later. But this is a neat hack when you know what you're doing and the proprietary API warning is just noise clogging the console. Thanks! – Boann Dec 05 '12 at 19:28
17

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.

JeeBee
  • 17,476
  • 5
  • 50
  • 60
7

because you cannot disable this warning

dfa
  • 114,442
  • 31
  • 189
  • 228
3

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)

Cowan
  • 37,227
  • 11
  • 66
  • 65
2

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

Quick interim fix on Linux:

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

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.

wolfy
  • 21
  • 1
2

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:

<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>
Brandon
  • 2,367
  • 26
  • 32
1

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

java.util.Base64
Z.I.J
  • 1,157
  • 16
  • 36
0

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
-3

The javac option -Xlint:unchecked does do the trick: it disables the warnings, see the javac manual for details

BertNase
  • 2,374
  • 20
  • 24