4

I have a class, which uses sun.rmi API (read below for reasons). I want to use java 14 for compilation of my project, but it should run on java 7 as well. So I try to use javac -release 7 flag to cross-compile across versions. Alas, compilation fails with

error: package sun.rmi.transport does not exist

Is there any way to let compiler now that this is OK? :)

I develop a java agent which should instrument RMI transport via bytecode generation to pass some data along with each RMI call. So please do not tell me "you REALLY should not depend on internal APIs".

Nikem
  • 5,716
  • 3
  • 32
  • 59
  • Why do you want to use JDK 14 if you target JDK 7? – Puce Apr 29 '20 at 14:21
  • @Puce I have several modules in my gradle project. Some of them target java7, some later versions. I would like to use a single compiler and just change target release on per-module basis. – Nikem Apr 29 '20 at 14:29
  • Have you tried using `--add-exports java.rmi/sun.rmi.transport=` ? – Jorn Vernee Apr 29 '20 at 14:31
  • 1
    @JornVernee Yes: `error: option --add-exports not allowed with target 7` – Nikem Apr 29 '20 at 14:32
  • @JornVernee Also, `error: package sun.rmi.transport does not exist` is not a sign of requiring an export. – Naman Apr 29 '20 at 14:40
  • 1
    Possibly related: https://stackoverflow.com/questions/4065401/using-internal-sun-classes-with-javac – Brian Goetz Apr 29 '20 at 16:07
  • @BrianGoetz certainly related, but that question was not helpful too :( `-XDignore.symbol.file` gives the same error – Nikem Apr 29 '20 at 16:57
  • 3
    Of course, `-XDignore.symbol.file` can't work with the `-release` option, as there is no alternative to the symbol file in that case. The `-release` option specifically tells the compiler to use the symbol file to find out which API artifacts are available. But you can always extend the set of available classes by putting a jar file containing a bunch of `sun.` classes having the desired properties on the class path while compiling. – Holger Apr 29 '20 at 19:10
  • You can always use plain old `--source 7 --target 7`. This will be not fully safe but this will solve your problem. Also, you can use `--source 7 --target 7 -Xbootclasspath:path_to_jdk7\jre\lib\rt.jar` which is a safer solution. – ZhekaKozlov May 29 '20 at 05:01

0 Answers0