Using internal sun classes with javac

JavaJavacInternal

Java Problem Overview


Is there a way to disable restrictions of javac 1.6.0_22 that prevent me from using JRE internal classes like sun.awt.event.* ?

I'm not looking for:

  1. an explanation why it is forbidden.
  2. suggestion to use different classes
  3. suggestion to use reflection
  4. suggestion to use ecj/eclipse

I just want to know if it is possible or not, and if it is then how.

Java Solutions


Solution 1 - Java

I have found the answer myself.

When javac is compiling code it doesn't link against rt.jar by default. Instead it uses special symbol file lib/ct.sym with class stubs.

Surprisingly this file contains many but not all of internal sun classes. In my case one of those more-internal-than-usual classes was sun.awt.event.IgnorePaintEvent.

And the answer to my question is: javac -XDignore.symbol.file

That's what javac uses for compiling rt.jar.

Solution 2 - Java

In addition to the answer by @marcin-wisnicki if you're using Maven, note that the compiler plugin will silently drop any -XD flags, unless you also specify <fork>true</fork>: e.g.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArgs>
                    <arg>-XDignore.symbol.file</arg>
                </compilerArgs>
                <fork>true</fork>
            </configuration>
            ...

Solution 3 - Java

There's a better solution. First add the option to javac -XDenableSunApiLintControl and then use @SupressWarnings("sunapi") in your code.

Solution 4 - Java

Normally, this only produces a Warning message; e.g.

[javac] /media/disk/opensso2/opensso/products/federation/openfm/source/com/sun/identity/wss/xmlsig/WSSSignatureProvider.java:46: warning: com.sun.org.apache.xpath.internal.XPathAPI is Sun proprietary API and may be removed in a future release
[javac] import com.sun.org.apache.xpath.internal.XPathAPI;

Perhaps you have told the Java compiler to treat warnings as errors.

Solution 5 - Java

If you are using Gradle, you need to use these options

compileJava {
    // enable using internal libraries
    options.fork = true
    options.forkOptions.executable = 'javac'
    options.compilerArgs << '-XDignore.symbol.file' }

Solution 6 - Java

Another way is change jdk.

In my case project java version 1.8. I used from jdk 11. Therefore This error has found in my project. So I changed my jdk from 11 to 1.8. It has worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcin WisnickiView Question on Stackoverflow
Solution 1 - JavaMarcin WisnickiView Answer on Stackoverflow
Solution 2 - JavakarmakazeView Answer on Stackoverflow
Solution 3 - JavajuancnView Answer on Stackoverflow
Solution 4 - JavaStephen CView Answer on Stackoverflow
Solution 5 - JavaKamiel AhmadpourView Answer on Stackoverflow
Solution 6 - JavaOrifjon YunusjanovView Answer on Stackoverflow