Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using internal sun classes with javac

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.

like image 532
Marcin Wisnicki Avatar asked Oct 31 '10 22:10

Marcin Wisnicki


People also ask

What is C in javac command?

The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.

What is the difference between Java and javac?

Technically, javac is the program that translates Java code into bytecode (. class file) - an intermediate format which is understandable by the Java Virtual Machine (JVM). And java is the program that starts the JVM, which in turn, loads the . class file, verifies the bytecode and executes it.


2 Answers

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.

like image 102
Marcin Wisnicki Avatar answered Oct 19 '22 23:10

Marcin Wisnicki


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>             ... 
like image 35
karmakaze Avatar answered Oct 19 '22 22:10

karmakaze