Cannot stop ant from generating compiler Sun propr

2019-01-15 18:56发布

问题:

I call javac from my ant script like this:

<javac srcdir="src" 
   destdir="build/classes" source="1.6" 
   target="1.6" debug="true" encoding="Cp1252"
   nowarn="true"> 

But it still throws compiler warnings in the output:

[javac] Compiling 73 source files to C:\IKOfficeRoot\Java\ERP\Framework\build\classes

[javac] C:\IKOfficeRoot\Java\ERP\Framework\src\de\ikoffice\util\LoggerFactory.java:49: warning: sun.reflect.Reflection is Sun proprietary API and may be removed in a future release
[javac]         return Logger.getLogger(Reflection.getCallerClass(2));
[javac]                                 ^
[javac] Note: C:\IKOfficeRoot\Java\ERP\Framework\src\de\ikoffice\db\SingleShotResultSet.java uses or overrides a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 warning

I also tried

<compilerarg line="-Xlint:-unchecked -Xlint:-deprecation"/>

and

<compilerarg value="-Xlint:none"/>

but this has no effect either. How to I remove the warnings?

回答1:

adding the -XDignore.symbol.file option to the javac command line worked for me.



回答2:

The best way to do this is to use the sunapi suppression, i.e. either -Xlint:-sunapi for the entire compile, or @SuppressWarnings("sunapi") at your required scope.

Note, the only way to use this suppression though, is to first enable it with the compiler option -XDenableSunApiLintControl

enableSunApiLintControl



回答3:

You cannot do that: Neither from the command line, so you cannot do it from Ant either.

The javac documentation says:

-Xlint:none

Disable all warnings not mandated by the Java Language Specification.

So it seems this kind of warning cannot be suppressed, because they are raised by the JLS directly.

What you can do is to:

  1. use @SuppressWarnings({"deprecation"}, {"unchecked"}) at the required positions
  2. use some workaround to redirect the warning messages to /dev/null
  3. try compiling from the command line with the -Xlint:-unchecked -Xlint:-deprecation arguments, maybe it is only an Ant-related issue.


回答4:

Recommendation: Don't ignore your compiler's warnings. The warnings are there for a reason. My company's legacy codebase is moving towards a "treat warnings as errors and fail the build" model as we can expend the effort to fix warnings produced during our compile cycle.

In your case, the warning is:

warning: sun.reflect.Reflection is Sun proprietary API and may be removed in a future release

It has always been a warning to not import from sun packages as long as I can remember, because they're a non-public API. There's even a FAQ about it on the official Oracle Java site.

You haven't posted your actual code, so it's hard to provide further recommendations...What are you using in sun.reflect.Reflection that you couldn't also do through something in java.lang.reflect?



回答5:

use nowarn attribute see below

e.g.

<javac srcdir="src" 
   destdir="build/classes" source="1.6" 
   target="1.6" debug="true" encoding="Cp1252"
   nowarn="on">

by default nowarn attribute is off



回答6:

To prevent deprecation warnings I tried to do it using JDK 6 and JDK 7.

With JDK 7, @Deprecated annotation does the work

With JDK 6, it does not work, neither adding -Xlint:-deprecation param works for me. The only way I managed to remove the warning was using the @deprecated Javadoc Tag:

  /**
   * @deprecated
   */
  @Override
  public void removeValue(String arg0) {
    // TODO Auto-generated method stub
  }